Browse Source

Check return value of SDL_small_alloc()

Fixes https://github.com/libsdl-org/SDL/issues/8959
Sam Lantinga 9 months ago
parent
commit
1cc85c912b

+ 3 - 0
src/SDL_log.c

@@ -696,6 +696,9 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
 #endif // !defined(SDL_PLATFORM_GDK)
 #endif // !defined(SDL_PLATFORM_GDK)
         length = SDL_strlen(GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
         length = SDL_strlen(GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
         output = SDL_small_alloc(char, length, &isstack);
         output = SDL_small_alloc(char, length, &isstack);
+        if (!output) {
+            return;
+        }
         (void)SDL_snprintf(output, length, "%s%s\r\n", GetLogPriorityPrefix(priority), message);
         (void)SDL_snprintf(output, length, "%s%s\r\n", GetLogPriorityPrefix(priority), message);
         tstr = WIN_UTF8ToString(output);
         tstr = WIN_UTF8ToString(output);
 
 

+ 1 - 2
src/render/SDL_render.c

@@ -3578,8 +3578,7 @@ bool SDL_RenderLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count
         bool isstack1;
         bool isstack1;
         bool isstack2;
         bool isstack2;
         float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
         float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
-        int *indices = SDL_small_alloc(int,
-                                       (4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
+        int *indices = SDL_small_alloc(int, (4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
 
 
         if (xy && indices) {
         if (xy && indices) {
             int i;
             int i;

+ 1 - 14
src/render/opengl/SDL_render_gl.c

@@ -1497,20 +1497,7 @@ static SDL_Surface *GL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *
 
 
     // Flip the rows to be top-down if necessary
     // Flip the rows to be top-down if necessary
     if (!renderer->target) {
     if (!renderer->target) {
-        bool isstack;
-        int length = rect->w * SDL_BYTESPERPIXEL(format);
-        Uint8 *src = (Uint8 *)surface->pixels + (rect->h - 1) * surface->pitch;
-        Uint8 *dst = (Uint8 *)surface->pixels;
-        Uint8 *tmp = SDL_small_alloc(Uint8, length, &isstack);
-        int rows = rect->h / 2;
-        while (rows--) {
-            SDL_memcpy(tmp, dst, length);
-            SDL_memcpy(dst, src, length);
-            SDL_memcpy(src, tmp, length);
-            dst += surface->pitch;
-            src -= surface->pitch;
-        }
-        SDL_small_free(tmp, isstack);
+        SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
     }
     }
     return surface;
     return surface;
 }
 }

+ 5 - 9
src/render/opengl/SDL_shaders_gl.c

@@ -348,15 +348,11 @@ static bool CompileShader(GL_ShaderContext *ctx, GLhandleARB shader, const char
 
 
         ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
         ctx->glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
         info = SDL_small_alloc(char, length + 1, &isstack);
         info = SDL_small_alloc(char, length + 1, &isstack);
-        ctx->glGetInfoLogARB(shader, length, NULL, info);
-        SDL_LogError(SDL_LOG_CATEGORY_RENDER,
-                     "Failed to compile shader:\n%s%s\n%s", defines, source, info);
-#ifdef DEBUG_SHADERS
-        fprintf(stderr,
-                "Failed to compile shader:\n%s%s\n%s", defines, source, info);
-#endif
-        SDL_small_free(info, isstack);
-
+        if (info) {
+            ctx->glGetInfoLogARB(shader, length, NULL, info);
+            SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Failed to compile shader:\n%s%s\n%s", defines, source, info);
+            SDL_small_free(info, isstack);
+        }
         return false;
         return false;
     } else {
     } else {
         return true;
         return true;

+ 1 - 14
src/render/opengles2/SDL_render_gles2.c

@@ -2014,20 +2014,7 @@ static SDL_Surface *GLES2_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rec
 
 
     // Flip the rows to be top-down if necessary
     // Flip the rows to be top-down if necessary
     if (!renderer->target) {
     if (!renderer->target) {
-        bool isstack;
-        int length = rect->w * SDL_BYTESPERPIXEL(format);
-        Uint8 *src = (Uint8 *)surface->pixels + (rect->h - 1) * surface->pitch;
-        Uint8 *dst = (Uint8 *)surface->pixels;
-        Uint8 *tmp = SDL_small_alloc(Uint8, length, &isstack);
-        int rows = rect->h / 2;
-        while (rows--) {
-            SDL_memcpy(tmp, dst, length);
-            SDL_memcpy(dst, src, length);
-            SDL_memcpy(src, tmp, length);
-            dst += surface->pitch;
-            src -= surface->pitch;
-        }
-        SDL_small_free(tmp, isstack);
+        SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
     }
     }
     return surface;
     return surface;
 }
 }

+ 1 - 15
src/render/vitagxm/SDL_render_vita_gxm.c

@@ -1103,22 +1103,8 @@ static SDL_Surface *VITA_GXM_RenderReadPixels(SDL_Renderer *renderer, const SDL_
     read_pixels(rect->x, y, rect->w, rect->h, surface->pixels);
     read_pixels(rect->x, y, rect->w, rect->h, surface->pixels);
 
 
     // Flip the rows to be top-down if necessary
     // Flip the rows to be top-down if necessary
-
     if (!renderer->target) {
     if (!renderer->target) {
-        bool isstack;
-        int length = rect->w * SDL_BYTESPERPIXEL(format);
-        Uint8 *src = (Uint8 *)surface->pixels + (rect->h - 1) * surface->pitch;
-        Uint8 *dst = (Uint8 *)surface->pixels;
-        Uint8 *tmp = SDL_small_alloc(Uint8, length, &isstack);
-        int rows = rect->h / 2;
-        while (rows--) {
-            SDL_memcpy(tmp, dst, length);
-            SDL_memcpy(dst, src, length);
-            SDL_memcpy(src, tmp, length);
-            dst += surface->pitch;
-            src -= surface->pitch;
-        }
-        SDL_small_free(tmp, isstack);
+        SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
     }
     }
     return surface;
     return surface;
 }
 }

+ 6 - 0
src/video/SDL_surface.c

@@ -1786,6 +1786,9 @@ static bool SDL_FlipSurfaceHorizontal(SDL_Surface *surface)
     bpp = SDL_BYTESPERPIXEL(surface->format);
     bpp = SDL_BYTESPERPIXEL(surface->format);
     row = (Uint8 *)surface->pixels;
     row = (Uint8 *)surface->pixels;
     tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
     tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
+    if (!tmp) {
+        return false;
+    }
     for (i = surface->h; i--; ) {
     for (i = surface->h; i--; ) {
         a = row;
         a = row;
         b = a + (surface->w - 1) * bpp;
         b = a + (surface->w - 1) * bpp;
@@ -1815,6 +1818,9 @@ static bool SDL_FlipSurfaceVertical(SDL_Surface *surface)
     a = (Uint8 *)surface->pixels;
     a = (Uint8 *)surface->pixels;
     b = a + (surface->h - 1) * surface->pitch;
     b = a + (surface->h - 1) * surface->pitch;
     tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
     tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
+    if (!tmp) {
+        return false;
+    }
     for (i = surface->h / 2; i--; ) {
     for (i = surface->h / 2; i--; ) {
         SDL_memcpy(tmp, a, surface->pitch);
         SDL_memcpy(tmp, a, surface->pitch);
         SDL_memcpy(a, b, surface->pitch);
         SDL_memcpy(a, b, surface->pitch);

+ 1 - 1
src/video/windows/SDL_windowsevents.c

@@ -1779,7 +1779,7 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
             UINT i, num_inputs = LOWORD(wParam);
             UINT i, num_inputs = LOWORD(wParam);
             bool isstack;
             bool isstack;
             PTOUCHINPUT inputs = SDL_small_alloc(TOUCHINPUT, num_inputs, &isstack);
             PTOUCHINPUT inputs = SDL_small_alloc(TOUCHINPUT, num_inputs, &isstack);
-            if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
+            if (inputs && data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
                 RECT rect;
                 RECT rect;
                 float x, y;
                 float x, y;
 
 

+ 3 - 0
src/video/windows/SDL_windowswindow.c

@@ -841,6 +841,9 @@ bool WIN_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *
     mask_len = (icon->h * (icon->w + 7) / 8);
     mask_len = (icon->h * (icon->w + 7) / 8);
     icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
     icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
     icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
     icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
+    if (!icon_bmp) {
+        return false;
+    }
 
 
     // Write the BITMAPINFO header
     // Write the BITMAPINFO header
     bmi = (BITMAPINFOHEADER *)icon_bmp;
     bmi = (BITMAPINFOHEADER *)icon_bmp;