Skip to content

Commit a89d6d9

Browse files
committed
Start error checking / polish efforts
1 parent 1f99dfa commit a89d6d9

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/IMG_png.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static struct {
117117
#endif
118118
int (*png_image_begin_read_from_memory) (png_imagep image, png_const_voidp memory, size_t size);
119119
int (*png_image_finish_read) (png_imagep image, png_const_colorp background, void *buffer, png_int_32 row_stride, void *colormap);
120+
void (*png_image_free) (png_imagep image);
120121
#if SDL_IMAGE_SAVE_PNG
121122
png_structp (*png_create_write_struct) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn);
122123
void (*png_destroy_write_struct) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr);
@@ -174,6 +175,7 @@ int IMG_InitPNG()
174175
#endif
175176
FUNCTION_LOADER(png_image_begin_read_from_memory, int (*) (png_imagep image, png_const_voidp memory, size_t size))
176177
FUNCTION_LOADER(png_image_finish_read, int (*) (png_imagep image, png_const_colorp background, void *buffer, png_int_32 row_stride, void *colormap))
178+
FUNCTION_LOADER(png_image_free, void (*) (png_imagep image))
177179
#if SDL_IMAGE_SAVE_PNG
178180
FUNCTION_LOADER(png_create_write_struct, png_structp (*) (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn))
179181
FUNCTION_LOADER(png_destroy_write_struct, void (*) (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr))
@@ -229,6 +231,7 @@ int IMG_isPNG(SDL_RWops *src)
229231
}
230232

231233
SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src) {
234+
Sint64 start;
232235
png_image image;
233236
Sint64 src_length;
234237
Uint8* raw_image_buffer;
@@ -237,28 +240,51 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src) {
237240
void* colormap = NULL;
238241
Uint32 pixelformat;
239242

240-
if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
241-
return NULL;
242-
}
243-
244243
/* This function uses libpng's "Simplified API" to read a PNG.
245244
* See https://github.com/pnggroup/libpng/blob/libpng16/libpng-manual.txt
246245
* And https://github.com/pnggroup/libpng/blob/libpng16/contrib/examples/pngtopng.c
247246
* For information about this and example usage, respectively. */
248247

248+
if ( !src ) {
249+
/* The error message has been set in SDL_RWFromFile */
250+
return NULL;
251+
}
252+
start = SDL_RWtell(src);
253+
if (start < 0) {
254+
return NULL; /*SDL error already set*/
255+
}
256+
257+
if ( (IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0 ) {
258+
return NULL;
259+
}
260+
249261
/* Only the image structure version number needs to be set. */
250262
SDL_memset(&image, 0, sizeof(image));
251263
image.version = PNG_IMAGE_VERSION;
252264

253-
src_length = SDL_RWsize(src); //todo error check
265+
src_length = SDL_RWsize(src);
266+
if (src_length < 0) {
267+
return NULL; /*SDL error already set*/
268+
}
269+
if (src_length == 0) {
270+
SDL_SetError("Cannot load PNG from length-0 RWops.");
271+
return NULL;
272+
}
254273

255-
printf("src_length=%i\n", src_length);
274+
/* Rewind to beginning of buffer to read entire thing. */
275+
if (SDL_RWseek(src, RW_SEEK_SET, 0) < 0) {
276+
return NULL; /*SDL error already set*/
277+
}
256278

257-
raw_image_buffer = SDL_malloc(src_length); // ec
258-
SDL_RWseek(src, RW_SEEK_SET, 0); // ec
279+
raw_image_buffer = SDL_malloc(src_length);
280+
if (raw_image_buffer == NULL) {
281+
SDL_OutOfMemory();
282+
return NULL;
283+
}
284+
259285
int objects_read = SDL_RWread(src, raw_image_buffer, 1, src_length); // ec
260286

261-
printf("objects_read=%i\n", objects_read);
287+
//printf("objects_read=%i\n", objects_read);
262288

263289
if (lib.png_image_begin_read_from_memory(&image, raw_image_buffer, src_length)) {
264290
/* If the image is natively encoded with a colormap, set the format to

0 commit comments

Comments
 (0)