From 1ef45c1801cc1713d0fa09bc87e64eaef425672c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 8 Nov 2020 23:40:17 -0800 Subject: [PATCH] Fixed bug 5339 - Minor memory leak in SDL_x11events.c wcodelyokoyt The atom name that X11_GetAtomName() returns never gets freed, which result in a minor memory leak (14 bytes?) every time the user drops a file on a window. You can see the line in question here: https://github.com/spurious/SDL-mirror/blob/6b6170caf69b4189c9a9d14fca96e97f09bbcc41/src/video/x11/SDL_x11events.c#L1350 Fix: call XFree on name after the while loop. --- src/video/x11/SDL_x11events.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index a4d8a9f03..c560b547b 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -1378,19 +1378,22 @@ X11_DispatchEvent(_THIS) X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY); if (p.format == 8) { - char* saveptr = NULL; - char* name = X11_XGetAtomName(display, target); - char *token = SDL_strtokr((char *) p.data, "\r\n", &saveptr); - while (token != NULL) { - if (SDL_strcmp("text/plain", name)==0) { - SDL_SendDropText(data->window, token); - } else if (SDL_strcmp("text/uri-list", name)==0) { - char *fn = X11_URIToLocal(token); - if (fn) { - SDL_SendDropFile(data->window, fn); + char *saveptr = NULL; + char *name = X11_XGetAtomName(display, target); + if (name) { + char *token = SDL_strtokr((char *) p.data, "\r\n", &saveptr); + while (token != NULL) { + if (SDL_strcmp("text/plain", name) == 0) { + SDL_SendDropText(data->window, token); + } else if (SDL_strcmp("text/uri-list", name) == 0) { + char *fn = X11_URIToLocal(token); + if (fn) { + SDL_SendDropFile(data->window, fn); + } } + token = SDL_strtokr(NULL, "\r\n", &saveptr); } - token = SDL_strtokr(NULL, "\r\n", &saveptr); + X11_XFree(name); } SDL_SendDropComplete(data->window); }