Date: Wed, 28 Jul 2004 14:56:57 +0800
authorSam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 13:10:58 +0000
changeset 944cdea7cbc3e23
parent 943 715c32d8f26c
child 945 d33645c36072
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
src/video/SDL_video.c
     1.1 --- a/src/video/SDL_RLEaccel.c	Sat Aug 21 12:38:50 2004 +0000
     1.2 +++ b/src/video/SDL_RLEaccel.c	Sat Aug 21 13:10:58 2004 +0000
     1.3 @@ -1832,7 +1832,7 @@
     1.4   * completely transparent pixels will be lost, and colour and alpha depth
     1.5   * may have been reduced (when encoding for 16bpp targets).
     1.6   */
     1.7 -static void UnRLEAlpha(SDL_Surface *surface)
     1.8 +static SDL_bool UnRLEAlpha(SDL_Surface *surface)
     1.9  {
    1.10      Uint8 *srcbuf;
    1.11      Uint32 *dst;
    1.12 @@ -1853,6 +1853,9 @@
    1.13      }
    1.14  
    1.15      surface->pixels = malloc(surface->h * surface->pitch);
    1.16 +    if ( !surface->pixels ) {
    1.17 +        return(SDL_FALSE);
    1.18 +    }
    1.19      /* fill background with transparent pixels */
    1.20      memset(surface->pixels, 0, surface->h * surface->pitch);
    1.21  
    1.22 @@ -1876,7 +1879,7 @@
    1.23  		srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
    1.24  		ofs += run;
    1.25  	    } else if(!ofs)
    1.26 -		return;
    1.27 +		return(SDL_TRUE);
    1.28  	} while(ofs < w);
    1.29  
    1.30  	/* skip padding if needed */
    1.31 @@ -1897,6 +1900,8 @@
    1.32  	} while(ofs < w);
    1.33  	dst += surface->pitch >> 2;
    1.34      }
    1.35 +    /* Make the compiler happy */
    1.36 +    return(SDL_TRUE);
    1.37  }
    1.38  
    1.39  void SDL_UnRLESurface(SDL_Surface *surface, int recode)
    1.40 @@ -1912,6 +1917,11 @@
    1.41  
    1.42  		/* re-create the original surface */
    1.43  		surface->pixels = malloc(surface->h * surface->pitch);
    1.44 +		if ( !surface->pixels ) {
    1.45 +			/* Oh crap... */
    1.46 +			surface->flags |= SDL_RLEACCEL;
    1.47 +			return;
    1.48 +		}
    1.49  
    1.50  		/* fill it with the background colour */
    1.51  		SDL_FillRect(surface, NULL, surface->format->colorkey);
    1.52 @@ -1924,8 +1934,13 @@
    1.53  		surface->flags &= ~SDL_SRCALPHA; /* opaque blit */
    1.54  		SDL_RLEBlit(surface, &full, surface, &full);
    1.55  		surface->flags |= alpha_flag;
    1.56 -	    } else
    1.57 -		UnRLEAlpha(surface);
    1.58 +	    } else {
    1.59 +		if ( !UnRLEAlpha(surface) ) {
    1.60 +		    /* Oh crap... */
    1.61 +		    surface->flags |= SDL_RLEACCEL;
    1.62 +		    return;
    1.63 +		}
    1.64 +	    }
    1.65  	}
    1.66  
    1.67  	if ( surface->map && surface->map->sw_data->aux_data ) {
     2.1 --- a/src/video/SDL_video.c	Sat Aug 21 12:38:50 2004 +0000
     2.2 +++ b/src/video/SDL_video.c	Sat Aug 21 13:10:58 2004 +0000
     2.3 @@ -1275,10 +1275,16 @@
     2.4  			/* Lazy physical palette allocation */
     2.5  			int size;
     2.6  			SDL_Palette *pp = malloc(sizeof(*pp));
     2.7 +			if ( !pp ) {
     2.8 +				return 0;
     2.9 +			}
    2.10  			current_video->physpal = pp;
    2.11  			pp->ncolors = pal->ncolors;
    2.12  			size = pp->ncolors * sizeof(SDL_Color);
    2.13  			pp->colors = malloc(size);
    2.14 +			if ( !pp->colors ) {
    2.15 +				return 0;
    2.16 +			}
    2.17  			memcpy(pp->colors, pal->colors, size);
    2.18  		}
    2.19  		if ( ! SetPalette_physical(screen,