Skip to content

Commit c8f81e7

Browse files
committed
Fix unittest failures on 2 PNG files with errors.
* Freeing already freed memory due to calling inflateIdat even when lodepng_decode_chunks has set error state. * Crash due to zlib decompressor zlib decompressor checking incoming size and, if > 2, accessing the data pointer even if its null. Fixed by initializing the size to 0 too.
1 parent 76f3fc1 commit c8f81e7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lodepng.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5304,7 +5304,7 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
53045304
LodePNGState* state,
53055305
const unsigned char* in, size_t insize) {
53065306
unsigned char IEND = 0;
5307-
unsigned char* idat;
5307+
unsigned char* idat = 0;
53085308
size_t idatsize = 0;
53095309
const unsigned char* chunk; /*points to beginning of next chunk*/
53105310

@@ -5317,6 +5317,7 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
53175317

53185318
/* safe output values in case error happens */
53195319
*idat_out = 0;
5320+
*idatsize_out = 0; /* zlib compressor checks the size rather than for a null pointer. */
53205321
*w = *h = 0;
53215322

53225323
state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/
@@ -5593,7 +5594,9 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
55935594
void* idat;
55945595
size_t idatsize;
55955596
(void)lodepng_decode_chunks(&idat, &idatsize, w, h, state, in, insize);
5596-
(void)inflateIdat(out, NULL, 0, *w, *h, state, idat, idatsize);
5597+
if (!state->error) {
5598+
(void)inflateIdat(out, NULL, 0, *w, *h, state, idat, idatsize);
5599+
}
55975600
}
55985601

55995602
unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h,

0 commit comments

Comments
 (0)