Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
authorSam Lantinga <slouken@libsdl.org>
Wed, 17 Apr 2013 01:35:10 -0700
changeset 7071 82f17e656125
parent 7070 65df21723f58
child 7072 1d9b4b7a8ce3
Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c Nitz In SDL_x11mouse.c file there is function named static Cursor X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y) { // Some code data_bits = SDL_calloc(1, surface->h * width_bytes); mask_bits = SDL_calloc(1, surface->h * width_bytes); if (!data_bits || !mask_bits) { SDL_OutOfMemory(); return None; } // Some code } Here is the problem in if statement, suppose if !data_bits is false and !mask_bits is true then, data_bits will go out of scope and leaks the memory it points to. Solution is that data_bits and mask_bits should be checked separately, not by using OR operator.
src/video/x11/SDL_x11mouse.c
--- a/src/video/x11/SDL_x11mouse.c	Wed Apr 17 01:32:06 2013 -0700
+++ b/src/video/x11/SDL_x11mouse.c	Wed Apr 17 01:35:10 2013 -0700
@@ -129,8 +129,14 @@
     unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
 
     data_bits = SDL_calloc(1, surface->h * width_bytes);
+    if (!data_bits) {
+        SDL_OutOfMemory();
+        return None;
+    }
+
     mask_bits = SDL_calloc(1, surface->h * width_bytes);
-    if (!data_bits || !mask_bits) {
+    if (!mask_bits) {
+        SDL_free(data_bits);
         SDL_OutOfMemory();
         return None;
     }