Skip to content

Commit

Permalink
Fixed buffer overflow in BMP loading code, discovered by j00ru//vx
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Nov 2, 2008
1 parent bbd9fd2 commit dc8debc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.7:
Sam Lantinga - Sun Nov 2 15:08:27 PST 2008
* Fixed buffer overflow in BMP loading code, discovered by j00ru//vx
Sam Lantinga - Fri Dec 28 08:34:54 PST 2007
* Fixed buffer overflow in GIF loading code, discovered by Michael Skladnikiewicz

Expand Down
22 changes: 14 additions & 8 deletions IMG_bmp.c
Expand Up @@ -68,11 +68,15 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
*/
int pitch = surface->pitch;
int height = surface->h;
Uint8 * bits = (Uint8 *)surface->pixels + ((height-1) * pitch);
Uint8 *start = (Uint8 *)surface->pixels;
Uint8 *end = start + (height*pitch);
Uint8 *bits = end-pitch, *spot;
int ofs = 0;
Uint8 ch;
Uint8 needsPad;

#define COPY_PIXEL(x) spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)

for (;;) {
if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
/*
Expand All @@ -84,15 +88,15 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
if ( isRle8 ) { /* 256-color bitmap, compressed */
do {
bits[ofs++] = pixel;
COPY_PIXEL(pixel);
} while (--ch);
}else { /* 16-color bitmap, compressed */
} else { /* 16-color bitmap, compressed */
Uint8 pixel0 = pixel >> 4;
Uint8 pixel1 = pixel & 0x0F;
for (;;) {
bits[ofs++] = pixel0; /* even count, high nibble */
COPY_PIXEL(pixel0); /* even count, high nibble */
if (!--ch) break;
bits[ofs++] = pixel1; /* odd count, low nibble */
COPY_PIXEL(pixel1); /* odd count, low nibble */
if (!--ch) break;
}
}
Expand Down Expand Up @@ -120,16 +124,18 @@ static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
if (isRle8) {
needsPad = ( ch & 1 );
do {
if ( !SDL_RWread(src, bits + ofs++, 1, 1) ) return 1;
Uint8 pixel;
if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
COPY_PIXEL(pixel);
} while (--ch);
} else {
needsPad = ( ((ch+1)>>1) & 1 ); /* (ch+1)>>1: bytes size */
for (;;) {
Uint8 pixel;
if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
bits[ofs++] = pixel >> 4;
COPY_PIXEL(pixel >> 4);
if (!--ch) break;
bits[ofs++] = pixel & 0x0F;
COPY_PIXEL(pixel & 0x0F);
if (!--ch) break;
}
}
Expand Down

0 comments on commit dc8debc

Please sign in to comment.