Check return value of SDL_small_alloc()

Fixes https://github.com/libsdl-org/SDL/issues/8959
This commit is contained in:
Sam Lantinga 2024-10-13 13:17:03 -07:00
parent d7be7fc168
commit 1cc85c912b
9 changed files with 22 additions and 55 deletions

View file

@ -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);

View file

@ -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, int *indices = SDL_small_alloc(int, (4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
(4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
if (xy && indices) { if (xy && indices) {
int i; int i;

View file

@ -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; SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
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);
} }
return surface; return surface;
} }

View file

@ -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); if (info) {
SDL_LogError(SDL_LOG_CATEGORY_RENDER, ctx->glGetInfoLogARB(shader, length, NULL, info);
"Failed to compile shader:\n%s%s\n%s", defines, source, info); SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Failed to compile shader:\n%s%s\n%s", defines, source, info);
#ifdef DEBUG_SHADERS SDL_small_free(info, isstack);
fprintf(stderr, }
"Failed to compile shader:\n%s%s\n%s", defines, source, info);
#endif
SDL_small_free(info, isstack);
return false; return false;
} else { } else {
return true; return true;

View file

@ -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; SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
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);
} }
return surface; return surface;
} }

View file

@ -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; SDL_FlipSurface(surface, SDL_FLIP_VERTICAL);
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);
} }
return surface; return surface;
} }

View file

@ -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);

View file

@ -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;

View file

@ -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;