From 32f6cbfb5c51081e9e2a882f877ea592b47c182f Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 25 Jun 2018 17:05:43 +0200 Subject: [PATCH] forbid GFX2_Surfaces with 0 pixels --- src/gfx2surface.c | 20 ++++++++------------ src/text.c | 7 +++++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/gfx2surface.c b/src/gfx2surface.c index 46f325fc..92315ebb 100644 --- a/src/gfx2surface.c +++ b/src/gfx2surface.c @@ -28,22 +28,18 @@ T_GFX2_Surface * New_GFX2_Surface(word width, word height) T_GFX2_Surface * surface; size_t size; + size = width * height; + if (size == 0) // surfaces with no pixels not allowed + return NULL; + surface = malloc(sizeof(T_GFX2_Surface)); if (surface == NULL) return NULL; - size = width * height; - if (size > 0) + surface->pixels = malloc(size); + if(surface->pixels == NULL) { - surface->pixels = malloc(size); - if(surface->pixels == NULL) - { - free(surface); - return NULL; - } - } - else - { - surface->pixels = NULL; + free(surface); + return NULL; } memset(surface->palette, 0, sizeof(surface->palette)); surface->w = width; diff --git a/src/text.c b/src/text.c index 697acc38..04fe7342 100644 --- a/src/text.c +++ b/src/text.c @@ -722,6 +722,13 @@ byte *Render_text_SFont(const char *str, int font_number, int *width, int *heigh *width = SFont_TextWidth(font, str); text_surface = New_GFX2_Surface(*width, *height); + if (text_surface == NULL) + { + Warning("Failed to allocate text surface"); + SFont_FreeFont(font); + Free_GFX2_Surface(font_surface); + return NULL; + } // Fill with transparent color memset(text_surface->pixels, font->Transparent, *width * *height); // Rendu du texte