@@ -5304,8 +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 = 0 ;
5308- size_t idatsize = 0 ;
5307+ unsigned char * idat;
53095308 const unsigned char * chunk; /* points to beginning of next chunk*/
53105309
53115310 /* for unknown chunk order*/
@@ -5314,7 +5313,6 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
53145313 unsigned critical_pos = 1 ; /* 1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/
53155314#endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
53165315
5317-
53185316 /* safe output values in case error happens */
53195317 *idat_out = 0 ;
53205318 *idatsize_out = 0 ; /* zlib compressor checks the size rather than for a null pointer. */
@@ -5328,8 +5326,9 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
53285326 }
53295327
53305328 /* the input filesize is a safe upper bound for the sum of idat chunks size*/
5331- idat = (unsigned char *)lodepng_malloc (insize);
5332- if (!idat) CERROR_RETURN_ERROR (state->error , 83 ); /* alloc fail*/
5329+ *idat_out = (unsigned char *)lodepng_malloc (insize);
5330+ if (!*idat_out) CERROR_RETURN_ERROR (state->error , 83 ); /* alloc fail*/
5331+ idat = *(unsigned char **)idat_out;
53335332
53345333 chunk = &in[33 ]; /* first byte of the first chunk after the header*/
53355334
@@ -5365,12 +5364,10 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
53655364 /* IDAT chunk, containing compressed image data*/
53665365 if (lodepng_chunk_type_equals (chunk, " IDAT" )) {
53675366 size_t newsize;
5368- if (lodepng_addofl (idatsize , chunkLength, &newsize)) CERROR_BREAK (state->error , 95 );
5367+ if (lodepng_addofl (*idatsize_out , chunkLength, &newsize)) CERROR_BREAK (state->error , 95 );
53695368 if (newsize > insize) CERROR_BREAK (state->error , 95 );
5370- lodepng_memcpy (idat + idatsize, data, chunkLength);
5371- idatsize += chunkLength;
5372- *idat_out = idat;
5373- *idatsize_out = idatsize;
5369+ lodepng_memcpy (idat + *idatsize_out, data, chunkLength);
5370+ *idatsize_out += chunkLength;
53745371#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
53755372 critical_pos = 3 ;
53765373#endif /* LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
@@ -5477,7 +5474,7 @@ unsigned lodepng_decode_chunks(void** idat_out, size_t* idatsize_out, unsigned*
54775474 if (!IEND) chunk = lodepng_chunk_next_const (chunk, in + insize);
54785475 }
54795476
5480- if (state->error ) lodepng_free (idat );
5477+ if (state->error ) lodepng_free (*idat_out );
54815478 return state->error ;
54825479}
54835480
@@ -5527,21 +5524,22 @@ static unsigned inflateIdat(unsigned char** out,
55275524 if (!state->error && scanlines_size != expected_size) state->error = 91 ; /* decompressed size doesn't match prediction*/
55285525 lodepng_free (idat);
55295526
5530- if (state->error )
5531- return state->error ;
5532-
5533- outsize = lodepng_get_raw_size (w, h, &state->info_png .color );
5534- if (out) {
5535- *out = (unsigned char *)lodepng_malloc (outsize);
5536- if (!*out) CERROR_RETURN_ERROR (state->error , 83 ); /* alloc fail*/
5537- dest = *out;
5538- } else {
5539- if (directOutSize < outsize) CERROR_RETURN_ERROR (state->error , 123 ); /* error: "destination buffer too small */
5540- dest = directOut;
5527+ if (!state->error ) {
5528+ outsize = lodepng_get_raw_size (w, h, &state->info_png .color );
5529+ if (out) {
5530+ *out = (unsigned char *)lodepng_malloc (outsize);
5531+ if (!*out) state->error = 83 ; /* alloc fail*/
5532+ dest = *out;
5533+ } else {
5534+ if (directOutSize < outsize) state->error = 123 ; /* error: "destination buffer too small */
5535+ dest = directOut;
5536+ }
55415537 }
55425538
5543- for (i = 0 ; i < outsize; i++) (dest)[i] = 0 ;
5544- state->error = postProcessScanlines (dest, scanlines, w, h, &state->info_png );
5539+ if (!state->error ) {
5540+ for (i = 0 ; i < outsize; i++) (dest)[i] = 0 ;
5541+ state->error = postProcessScanlines (dest, scanlines, w, h, &state->info_png );
5542+ }
55455543 lodepng_free (scanlines);
55465544 return state->error ;
55475545}
0 commit comments