1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-25 22:18:30 +02:00

LoadImage: Check LodePNG return value and print errors

Dvoid from Discord just reported a crash when trying to load a
custom tiles2.png that was encoded weirdly.

The problem is that we don't check the return value from LodePNG, so
LodePNG gives us a null pointer, and then
SDL_CreateRGBSurfaceWithFormatFrom doesn't check this null pointer,
which then propagates until we crash in SDL_ConvertSurfaceFormat (or
rather, one of its sub-functions), and we would probably crash somewhere
else anyway if it continued.

After properly checking LodePNG's return value, along with printing the
error, it turns out that Dvoid's custom tiles2.png had an "invalid CRC".
I don't know what this means but it sounds worrying. `feh` can read the
file correctly but it also reports a "CRC error".
This commit is contained in:
Misa 2021-11-14 14:02:51 -08:00
parent a6728c4648
commit 6e832cae20

View File

@ -20,6 +20,7 @@ extern "C"
const unsigned char* in,
size_t insize
);
extern const char* lodepng_error_text(unsigned code);
}
static SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool noAlpha = false)
@ -31,6 +32,7 @@ static SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool no
unsigned char *data;
unsigned int width, height;
unsigned int error;
unsigned char *fileIn;
size_t length;
@ -42,14 +44,20 @@ static SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool no
}
if (noAlpha)
{
lodepng_decode24(&data, &width, &height, fileIn, length);
error = lodepng_decode24(&data, &width, &height, fileIn, length);
}
else
{
lodepng_decode32(&data, &width, &height, fileIn, length);
error = lodepng_decode32(&data, &width, &height, fileIn, length);
}
FILESYSTEM_freeMemory(&fileIn);
if (error != 0)
{
vlog_error("Could not load %s: %s", filename, lodepng_error_text(error));
return NULL;
}
loadedImage = SDL_CreateRGBSurfaceWithFormatFrom(
data,
width,