From 99468fc2a7d0ba8a62c91f79f7a47c711ae21814 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 11 Jul 2024 17:21:58 -0700 Subject: [PATCH] Do error checking when allocating a palette --- src/SDL_ttf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/SDL_ttf.c b/src/SDL_ttf.c index f66d5d0b..b1724f67 100644 --- a/src/SDL_ttf.c +++ b/src/SDL_ttf.c @@ -1444,14 +1444,21 @@ static SDL_Surface *AllocateAlignedPixels(size_t width, size_t height, SDL_Pixel SDL_aligned_free(pixels); return NULL; } - if (SDL_ISPIXELFORMAT_INDEXED(format)) { - SDL_SetSurfacePalette(textbuf, SDL_CreatePalette(1 << SDL_BITSPERPIXEL(format))); - } /* Let SDL handle the memory allocation */ textbuf->flags &= ~SDL_SURFACE_PREALLOCATED; textbuf->flags |= SDL_SURFACE_SIMD_ALIGNED; + /* Allocate a palette if needed */ + if (SDL_ISPIXELFORMAT_INDEXED(format)) { + SDL_Palette *palette = SDL_CreatePalette(1 << SDL_BITSPERPIXEL(format)); + if (!palette) { + SDL_DestroySurface(textbuf); + return NULL; + } + SDL_SetSurfacePalette(textbuf, palette); + } + if (bytes_per_pixel == 4) { SDL_memset4(pixels, bgcolor, size / 4); }