SDL: clipboard: Fixes additional x11 clipboard bugs found in tests

From 6ab846b6885a147013527d187c21eb8f686ca615 Mon Sep 17 00:00:00 2001
From: Linus Probert <[EMAIL REDACTED]>
Date: Mon, 12 Jun 2023 18:27:40 +0200
Subject: [PATCH] clipboard: Fixes additional x11 clipboard bugs found in tests

---
 src/video/x11/SDL_x11clipboard.c | 12 ++++++++----
 test/testautomation_clipboard.c  |  2 +-
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/video/x11/SDL_x11clipboard.c b/src/video/x11/SDL_x11clipboard.c
index cee4c664da7f..7cd58a0ad3c6 100644
--- a/src/video/x11/SDL_x11clipboard.c
+++ b/src/video/x11/SDL_x11clipboard.c
@@ -170,6 +170,7 @@ static void *GetSelectionData(SDL_VideoDevice *_this, Atom selection_type, size_
     Uint64 waitStart;
     Uint64 waitElapsed;
 
+    SDLX11_ClipboardData *clipboard;
     void *data = NULL;
     unsigned char *src = NULL;
     Atom XA_MIME = X11_XInternAtom(display, mime_type, False);
@@ -185,12 +186,15 @@ static void *GetSelectionData(SDL_VideoDevice *_this, Atom selection_type, size_
     } else if (owner == window) {
         owner = DefaultRootWindow(display);
         if (selection_type == XA_PRIMARY) {
-            src = videodata->primary_selection.callback(length, mime_type, videodata->primary_selection.userdata);
+            clipboard = &videodata->primary_selection;
         } else {
-            src = videodata->clipboard.callback(length, mime_type, videodata->clipboard.userdata);
+            clipboard = &videodata->clipboard;
         }
 
-        data = CloneDataBuffer(src, length, nullterminate);
+        if (clipboard->callback) {
+            src = clipboard->callback(length, mime_type, clipboard->userdata);
+            data = CloneDataBuffer(src, length, nullterminate);
+        }
     } else {
         /* Request that the selection owner copy the data to our window */
         owner = window;
@@ -264,7 +268,7 @@ SDL_bool X11_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type)
     size_t length;
     void *data;
     data = X11_GetClipboardData(_this, &length, mime_type);
-    if (data != NULL && length > 0) {
+    if (data != NULL) {
         SDL_free(data);
     }
     return length > 0;
diff --git a/test/testautomation_clipboard.c b/test/testautomation_clipboard.c
index 8d9a229a2571..41289a12cb39 100644
--- a/test/testautomation_clipboard.c
+++ b/test/testautomation_clipboard.c
@@ -30,7 +30,7 @@ static int clipboard_testHasClipboardText(void *arg)
 static int clipboard_testHasClipboardData(void *arg)
 {
     SDL_HasClipboardData("image/png");
-    SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
+    SDLTest_AssertPass("Call to SDL_HasClipboardData succeeded");
 
     return TEST_COMPLETED;
 }