From 1cc85c912bb3352a121b1fdc181c6ab6546157df Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 13 Oct 2024 13:17:03 -0700
Subject: [PATCH] Check return value of SDL_small_alloc()
Fixes https://github.com/libsdl-org/SDL/issues/8959
---
src/SDL_log.c | 3 +++
src/render/SDL_render.c | 3 +--
src/render/opengl/SDL_render_gl.c | 15 +--------------
src/render/opengl/SDL_shaders_gl.c | 14 +++++---------
src/render/opengles2/SDL_render_gles2.c | 15 +--------------
src/render/vitagxm/SDL_render_vita_gxm.c | 16 +---------------
src/video/SDL_surface.c | 6 ++++++
src/video/windows/SDL_windowsevents.c | 2 +-
src/video/windows/SDL_windowswindow.c | 3 +++
9 files changed, 22 insertions(+), 55 deletions(-)
diff --git a/src/SDL_log.c b/src/SDL_log.c
index cb619d120ed93..f9598487ccdc5 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -696,6 +696,9 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
#endif // !defined(SDL_PLATFORM_GDK)
length = SDL_strlen(GetLogPriorityPrefix(priority)) + SDL_strlen(message) + 1 + 1 + 1;
output = SDL_small_alloc(char, length, &isstack);
+ if (!output) {
+ return;
+ }
(void)SDL_snprintf(output, length, "%s%s\r\n", GetLogPriorityPrefix(priority), message);
tstr = WIN_UTF8ToString(output);
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index aec677630f790..992beaeb52182 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -3578,8 +3578,7 @@ bool SDL_RenderLines(SDL_Renderer *renderer, const SDL_FPoint *points, int count
bool isstack1;
bool isstack2;
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) {
int i;
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index 627c74e445d3c..b598098a599f1 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/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
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;
}
diff --git a/src/render/opengl/SDL_shaders_gl.c b/src/render/opengl/SDL_shaders_gl.c
index d9600f59d5eb4..c876fb98290b9 100644
--- a/src/render/opengl/SDL_shaders_gl.c
+++ b/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);
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;
} else {
return true;
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index a779ff3122646..37aab0703c064 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/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
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;
}
diff --git a/src/render/vitagxm/SDL_render_vita_gxm.c b/src/render/vitagxm/SDL_render_vita_gxm.c
index 559a15266cde1..87487beb406bd 100644
--- a/src/render/vitagxm/SDL_render_vita_gxm.c
+++ b/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);
// Flip the rows to be top-down if necessary
-
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;
}
diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index 40847e3902dce..657d7f462cb2c 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -1786,6 +1786,9 @@ static bool SDL_FlipSurfaceHorizontal(SDL_Surface *surface)
bpp = SDL_BYTESPERPIXEL(surface->format);
row = (Uint8 *)surface->pixels;
tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
+ if (!tmp) {
+ return false;
+ }
for (i = surface->h; i--; ) {
a = row;
b = a + (surface->w - 1) * bpp;
@@ -1815,6 +1818,9 @@ static bool SDL_FlipSurfaceVertical(SDL_Surface *surface)
a = (Uint8 *)surface->pixels;
b = a + (surface->h - 1) * surface->pitch;
tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
+ if (!tmp) {
+ return false;
+ }
for (i = surface->h / 2; i--; ) {
SDL_memcpy(tmp, a, surface->pitch);
SDL_memcpy(a, b, surface->pitch);
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index d383dc23c995e..fe505812d894d 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/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);
bool 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;
float x, y;
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 75c75ed972a45..6dbb001a6ee34 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/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);
icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
+ if (!icon_bmp) {
+ return false;
+ }
// Write the BITMAPINFO header
bmi = (BITMAPINFOHEADER *)icon_bmp;