SDL: wayland: Don't double-free clipboard sources if ours gets cancelled.

From a6179e85c02bc65ff64553c046b16d2ef1a72ba9 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 5 Aug 2022 21:23:39 -0400
Subject: [PATCH] wayland: Don't double-free clipboard sources if ours gets
 cancelled.

Fixes #6007.
---
 src/video/wayland/SDL_waylanddatamanager.c | 5 +++++
 src/video/wayland/SDL_waylanddatamanager.h | 1 +
 2 files changed, 6 insertions(+)

diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c
index c8d3ec757e4..3fbdd72fabc 100644
--- a/src/video/wayland/SDL_waylanddatamanager.c
+++ b/src/video/wayland/SDL_waylanddatamanager.c
@@ -322,6 +322,10 @@ void
 Wayland_data_source_destroy(SDL_WaylandDataSource *source)
 {
     if (source != NULL) {
+        SDL_WaylandDataDevice *data_device = (SDL_WaylandDataDevice *) source->data_device;
+        if (data_device && (data_device->selection_source == source)) {
+            data_device->selection_source = NULL;
+        }
         wl_data_source_destroy(source->source);
         mime_data_list_free(&source->mimes);
         SDL_free(source);
@@ -449,6 +453,7 @@ Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
                 Wayland_data_source_destroy(data_device->selection_source);
             }
             data_device->selection_source = source;
+            source->data_device = data_device;
         }
     }
 
diff --git a/src/video/wayland/SDL_waylanddatamanager.h b/src/video/wayland/SDL_waylanddatamanager.h
index 4e85dfa3109..0030a391553 100644
--- a/src/video/wayland/SDL_waylanddatamanager.h
+++ b/src/video/wayland/SDL_waylanddatamanager.h
@@ -40,6 +40,7 @@ typedef struct {
 typedef struct {
     struct wl_data_source *source;
     struct wl_list mimes;
+    void *data_device;
 } SDL_WaylandDataSource;
 
 typedef struct {