[SDL] Crash bug in SDL_SetGammaRamp()

Ken Rogoway Ken at HomebrewSoftware.com
Sun Aug 12 16:41:25 PDT 2007


Below is a fix for SDL_SetGammaRamp().  The problem I ran into is that pal
can be NULL and that crashes when trying to dereference elements in the
SDL_Palette structure.  The fix is listed below.  Search for BUGFIX to find
the line I changed.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

 
int SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16
*blue)
{
	int succeeded;
	SDL_VideoDevice *video = current_video;
	SDL_VideoDevice *this  = current_video;	
	SDL_Surface *screen = SDL_PublicSurface;

	/* Verify the screen parameter */
	if ( !screen ) {
		SDL_SetError("No video mode has been set");
		return -1;
	}

	/* Lazily allocate the gamma tables */
	if ( ! video->gamma ) {
		SDL_GetGammaRamp(0, 0, 0);
	}

	/* Fill the gamma table with the new values */
	if ( red ) {
		SDL_memcpy(&video->gamma[0*256], red,
256*sizeof(*video->gamma));
	}
	if ( green ) {
		SDL_memcpy(&video->gamma[1*256], green,
256*sizeof(*video->gamma));
	}
	if ( blue ) {
		SDL_memcpy(&video->gamma[2*256], blue,
256*sizeof(*video->gamma));
	}

	/* Gamma correction always possible on split palettes */
	if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
		SDL_Palette *pal = screen->format->palette;

		/* If physical palette has been set independently, use it */
		if(video->physpal)
		        pal = video->physpal;

		// BUGFIX: pal can be NULL.
		if ( pal ) {
			SDL_SetPalette(screen, SDL_PHYSPAL,
				pal->colors, 0, pal->ncolors);
		}
		return 0;
	}

	/* Try to set the gamma ramp in the driver */
	succeeded = -1;
	if ( video->SetGammaRamp ) {
		succeeded = video->SetGammaRamp(this, video->gamma);
	} else {
		SDL_SetError("Gamma ramp manipulation not supported");
	}
	return succeeded;
}

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.476 / Virus Database: 269.11.15/949 - Release Date: 8/12/2007
11:03 AM
 



More information about the SDL mailing list