Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set hard limit for image buffer size #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5003,16 +5003,30 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
expected_size += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, bpp);
}

if(expected_size > LODEPNG_IMAGE_DATA_SIZE_MAX) {
state->error = 116;
}
}

if (!state->error) {
state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, &state->decoder.zlibsettings);
}

if(!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn't match prediction*/
lodepng_free(idat);

if(!state->error) {
outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color);
if (outsize > LODEPNG_IMAGE_DATA_SIZE_MAX) {
state->error = 116;
}
}

if(!state->error) {
*out = (unsigned char*)lodepng_malloc(outsize);
if(!*out) state->error = 83; /*alloc fail*/
}

if(!state->error) {
lodepng_memset(*out, 0, outsize);
state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png);
Expand Down Expand Up @@ -6446,6 +6460,8 @@ const char* lodepng_error_text(unsigned code) {
case 113: return "ICC profile unreasonably large";
case 114: return "sBIT chunk has wrong size for the color type of the image";
case 115: return "sBIT value out of range";
/*max size of an in-memory image buffer*/
case 116: return "image data unreasonably large";
}
return "unknown error code";
}
Expand Down
4 changes: 4 additions & 0 deletions lodepng.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ freely, subject to the following restrictions:

extern const char* LODEPNG_VERSION_STRING;

/*Hard upper limit on size of an uncompressed in-memory image buffer. The
total memory consumption may be higher, e.g. during postProcessScanlines().*/
#define LODEPNG_IMAGE_DATA_SIZE_MAX 0xffffffffU

/*
The following #defines are used to create code sections. They can be disabled
to disable code sections, which can give faster compile time and smaller binary.
Expand Down