Commit ca46804210afca3919ace91e25b9b3e778d74729

Ryan C. Gordon 2015-08-07T00:59:31

X11: Fixed a memory leak when adding duplicate display modes.

diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c
index 4a91f07..b184dd8 100644
--- a/src/video/x11/SDL_x11modes.c
+++ b/src/video/x11/SDL_x11modes.c
@@ -748,7 +748,9 @@ X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
                 *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
             }
             mode.driverdata = modedata;
-            SDL_AddDisplayMode(sdl_display, &mode);
+            if (!SDL_AddDisplayMode(sdl_display, &mode)) {
+                SDL_free(modedata);
+            }
         }
         else if (!data->use_xrandr)
         {
@@ -762,7 +764,9 @@ X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
                 *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
             }
             mode.driverdata = modedata;
-            SDL_AddDisplayMode(sdl_display, &mode);
+            if (!SDL_AddDisplayMode(sdl_display, &mode)) {
+                SDL_free(modedata);
+            }
         }
 
     }
@@ -787,9 +791,8 @@ X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
                     }
                     mode.driverdata = modedata;
 
-                    if (SetXRandRModeInfo(display, res, output_info, output_info->modes[i], &mode)) {
-                        SDL_AddDisplayMode(sdl_display, &mode);
-                    } else {
+                    if (!SetXRandRModeInfo(display, res, output_info, output_info->modes[i], &mode) ||
+                        !SDL_AddDisplayMode(sdl_display, &mode)) {
                         SDL_free(modedata);
                     }
                 }
@@ -822,9 +825,7 @@ X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
             }
             mode.driverdata = modedata;
 
-            if (SetXVidModeModeInfo(modes[i], &mode)) {
-                SDL_AddDisplayMode(sdl_display, &mode);
-            } else {
+            if (!SetXVidModeModeInfo(modes[i], &mode) || !SDL_AddDisplayMode(sdl_display, &mode)) {
                 SDL_free(modedata);
             }
         }
@@ -842,7 +843,9 @@ X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
             *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
         }
         mode.driverdata = modedata;
-        SDL_AddDisplayMode(sdl_display, &mode);
+        if (!SDL_AddDisplayMode(sdl_display, &mode)) {
+            SDL_free(modedata);
+        }
     }
 }