1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-05 02:39:41 +01:00

Fix dereferencing NULL if image fails to load

If LoadImage() returned NULL, the game would dereference it and
segfault. So I've added NULL checks to dereferencing the pointers.
This commit is contained in:
Misa 2021-04-18 10:57:48 -07:00 committed by Ethan Lee
parent 8956b04d67
commit 9f11438dcc

View file

@ -306,15 +306,19 @@ void Graphics::updatetitlecolours(void)
}
#define PROCESS_TILESHEET_CHECK_ERROR(tilesheet, tile_square) \
if (grphx.im_##tilesheet->w % tile_square != 0 \
if (grphx.im_##tilesheet == NULL) \
{ \
/* We have already asserted; just no-op. */ \
} \
else if (grphx.im_##tilesheet->w % tile_square != 0 \
|| grphx.im_##tilesheet->h % tile_square != 0) \
{ \
const char* error = "Error: %s.png dimensions not exact multiples of %i!"; \
char message[128]; \
SDL_snprintf(message, sizeof(message), error, #tilesheet, tile_square); \
\
const char* error_title = "Error with %s.png"; \
char message_title[128]; \
\
SDL_snprintf(message, sizeof(message), error, #tilesheet, tile_square); \
SDL_snprintf(message_title, sizeof(message_title), error_title, #tilesheet); \
\
puts(message); \
@ -332,23 +336,28 @@ void Graphics::updatetitlecolours(void)
#define PROCESS_TILESHEET_RENAME(tilesheet, vector, tile_square, extra_code) \
PROCESS_TILESHEET_CHECK_ERROR(tilesheet, tile_square) \
\
for (int j = 0; j < grphx.im_##tilesheet->h / tile_square; j++) \
else \
{ \
for (int i = 0; i < grphx.im_##tilesheet->w / tile_square; i++) \
int j; \
for (j = 0; j < grphx.im_##tilesheet->h / tile_square; ++j) \
{ \
SDL_Surface* temp = GetSubSurface( \
grphx.im_##tilesheet, \
i * tile_square, j * tile_square, \
tile_square, tile_square \
); \
vector.push_back(temp); \
\
extra_code \
int i; \
for (i = 0; i < grphx.im_##tilesheet->w / tile_square; ++i) \
{ \
SDL_Surface* temp = GetSubSurface( \
grphx.im_##tilesheet, \
i * tile_square, j * tile_square, \
tile_square, tile_square \
); \
vector.push_back(temp); \
\
extra_code \
} \
} \
} \
\
SDL_FreeSurface(grphx.im_##tilesheet); \
grphx.im_##tilesheet = NULL;
\
SDL_FreeSurface(grphx.im_##tilesheet); \
grphx.im_##tilesheet = NULL; \
}
#define PROCESS_TILESHEET(tilesheet, tile_square, extra_code) \
PROCESS_TILESHEET_RENAME(tilesheet, tilesheet, tile_square, extra_code)