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 707182f17e656125
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
     1.1 --- a/src/video/x11/SDL_x11mouse.c	Wed Apr 17 01:32:06 2013 -0700
     1.2 +++ b/src/video/x11/SDL_x11mouse.c	Wed Apr 17 01:35:10 2013 -0700
     1.3 @@ -129,8 +129,14 @@
     1.4      unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
     1.5  
     1.6      data_bits = SDL_calloc(1, surface->h * width_bytes);
     1.7 +    if (!data_bits) {
     1.8 +        SDL_OutOfMemory();
     1.9 +        return None;
    1.10 +    }
    1.11 +
    1.12      mask_bits = SDL_calloc(1, surface->h * width_bytes);
    1.13 -    if (!data_bits || !mask_bits) {
    1.14 +    if (!mask_bits) {
    1.15 +        SDL_free(data_bits);
    1.16          SDL_OutOfMemory();
    1.17          return None;
    1.18      }