SDL: x11: Fix memory leaks in clipboard event handler

From bbc9c75618fbc60583123e03f31f4e856f2654bf Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Tue, 26 Nov 2024 10:54:59 -0500
Subject: [PATCH] x11: Fix memory leaks in clipboard event handler

XGetWindowProperty and XGetAtomName return data which must be freed by the client, and was being leaked.
---
 src/video/x11/SDL_x11events.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index 562a335c6e6e0..c6dee1d638dbf 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -650,7 +650,9 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
 
             int allocationsize = (length + 1) * sizeof(char*);
             for (j = 0, patom = (Atom*)data; j < length; j++, patom++) {
-                allocationsize += SDL_strlen( X11_XGetAtomName(display, *patom) ) + 1;
+                char *atomStr = X11_XGetAtomName(display, *patom);
+                allocationsize += SDL_strlen(atomStr) + 1;
+                X11_XFree(atomStr);
             }
 
             char **new_mime_types = SDL_AllocateTemporaryMemory(allocationsize);
@@ -658,13 +660,19 @@ static void X11_HandleClipboardEvent(SDL_VideoDevice *_this, const XEvent *xeven
                 char *strPtr = (char *)(new_mime_types + length + 1);
 
                 for (j = 0, patom = (Atom*)data; j < length; j++, patom++) {
+                    char *atomStr = X11_XGetAtomName(display, *patom);
                     new_mime_types[j] = strPtr;
-                    strPtr = stpcpy(strPtr, X11_XGetAtomName(display, *patom)) + 1;
+                    strPtr = stpcpy(strPtr, atomStr) + 1;
+                    X11_XFree(atomStr);
                 }
                 new_mime_types[length] = NULL;
 
                 SDL_SendClipboardUpdate(false, new_mime_types, length);
             }
+
+            if (data) {
+                X11_XFree(data);
+            }
         }
 
         videodata->selection_waiting = false;