From 879c3f8fd35e596ba170e57a695516b77079d426 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 21 Aug 2004 13:10:58 +0000 Subject: [PATCH] Date: Wed, 28 Jul 2004 14:56:57 +0800 From: Aaron Perez Subject: [SDL] Fwd: SDL not checking malloc returning NULL I was reading through the code and i found that in several places does a malloc and without checking if it is NULL just use the pointer. --- src/video/SDL_RLEaccel.c | 23 +++++++++++++++++++---- src/video/SDL_video.c | 6 ++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/video/SDL_RLEaccel.c b/src/video/SDL_RLEaccel.c index 72865c511..f6c38c71f 100644 --- a/src/video/SDL_RLEaccel.c +++ b/src/video/SDL_RLEaccel.c @@ -1832,7 +1832,7 @@ int SDL_RLESurface(SDL_Surface *surface) * completely transparent pixels will be lost, and colour and alpha depth * may have been reduced (when encoding for 16bpp targets). */ -static void UnRLEAlpha(SDL_Surface *surface) +static SDL_bool UnRLEAlpha(SDL_Surface *surface) { Uint8 *srcbuf; Uint32 *dst; @@ -1853,6 +1853,9 @@ static void UnRLEAlpha(SDL_Surface *surface) } surface->pixels = malloc(surface->h * surface->pitch); + if ( !surface->pixels ) { + return(SDL_FALSE); + } /* fill background with transparent pixels */ memset(surface->pixels, 0, surface->h * surface->pitch); @@ -1876,7 +1879,7 @@ static void UnRLEAlpha(SDL_Surface *surface) srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf); ofs += run; } else if(!ofs) - return; + return(SDL_TRUE); } while(ofs < w); /* skip padding if needed */ @@ -1897,6 +1900,8 @@ static void UnRLEAlpha(SDL_Surface *surface) } while(ofs < w); dst += surface->pitch >> 2; } + /* Make the compiler happy */ + return(SDL_TRUE); } void SDL_UnRLESurface(SDL_Surface *surface, int recode) @@ -1912,6 +1917,11 @@ void SDL_UnRLESurface(SDL_Surface *surface, int recode) /* re-create the original surface */ surface->pixels = malloc(surface->h * surface->pitch); + if ( !surface->pixels ) { + /* Oh crap... */ + surface->flags |= SDL_RLEACCEL; + return; + } /* fill it with the background colour */ SDL_FillRect(surface, NULL, surface->format->colorkey); @@ -1924,8 +1934,13 @@ void SDL_UnRLESurface(SDL_Surface *surface, int recode) surface->flags &= ~SDL_SRCALPHA; /* opaque blit */ SDL_RLEBlit(surface, &full, surface, &full); surface->flags |= alpha_flag; - } else - UnRLEAlpha(surface); + } else { + if ( !UnRLEAlpha(surface) ) { + /* Oh crap... */ + surface->flags |= SDL_RLEACCEL; + return; + } + } } if ( surface->map && surface->map->sw_data->aux_data ) { diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 197982080..1980bd8af 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -1275,10 +1275,16 @@ int SDL_SetPalette(SDL_Surface *screen, int which, /* Lazy physical palette allocation */ int size; SDL_Palette *pp = malloc(sizeof(*pp)); + if ( !pp ) { + return 0; + } current_video->physpal = pp; pp->ncolors = pal->ncolors; size = pp->ncolors * sizeof(SDL_Color); pp->colors = malloc(size); + if ( !pp->colors ) { + return 0; + } memcpy(pp->colors, pal->colors, size); } if ( ! SetPalette_physical(screen,