Commit fe586d077fe6b13cf0a33aa601446d7fe6d64b68

Philipp Wiesemann 2015-02-08T21:25:37

Fixed three memory leaks on failed allocation.

diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 32eb0f9..0ce443a 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -303,10 +303,11 @@ static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
 {
     if (button >= mouse->num_clickstates) {
         int i, count = button + 1;
-        mouse->clickstate = (SDL_MouseClickState *)SDL_realloc(mouse->clickstate, count * sizeof(*mouse->clickstate));
-        if (!mouse->clickstate) {
+        SDL_MouseClickState *clickstate = (SDL_MouseClickState *)SDL_realloc(mouse->clickstate, count * sizeof(*mouse->clickstate));
+        if (!clickstate) {
             return NULL;
         }
+        mouse->clickstate = clickstate;
 
         for (i = mouse->num_clickstates; i < count; ++i) {
             SDL_zero(mouse->clickstate[i]);
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index ecd0d53..f34bf12 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -342,9 +342,10 @@ GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GL
 
     if (type == GL_DEBUG_TYPE_ERROR_ARB) {
         /* Record this error */
+        char **error_messages = SDL_realloc(data->error_messages, data->errors * sizeof(*data->error_messages));
         ++data->errors;
-        data->error_messages = SDL_realloc(data->error_messages, data->errors * sizeof(*data->error_messages));
-        if (data->error_messages) {
+        if (error_messages) {
+            data->error_messages = error_messages;
             data->error_messages[data->errors-1] = SDL_strdup(message);
         }
     }
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
index fcc48c6..c5de8f0 100644
--- a/src/video/SDL_bmp.c
+++ b/src/video/SDL_bmp.c
@@ -306,16 +306,18 @@ SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
             biClrUsed = 1 << biBitCount;
         }
         if ((int) biClrUsed > palette->ncolors) {
+            SDL_Color *colors;
             palette->ncolors = biClrUsed;
-            palette->colors =
+            colors =
                 (SDL_Color *) SDL_realloc(palette->colors,
                                           palette->ncolors *
                                           sizeof(*palette->colors));
-            if (!palette->colors) {
+            if (!colors) {
                 SDL_OutOfMemory();
                 was_error = SDL_TRUE;
                 goto done;
             }
+            palette->colors = colors;
         } else if ((int) biClrUsed < palette->ncolors) {
             palette->ncolors = biClrUsed;
         }