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

AddressSanitizer: fix allocating too big buffers (libjpeg-turbo) #893

Merged
Merged
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
25 changes: 20 additions & 5 deletions src/pxUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ rtError pxLoadAImage(const char* imageData, size_t imageDataSize,
s.init();
s.addBuffer(o,0);
}
return retVal;

return retVal;
}


Expand Down Expand Up @@ -359,7 +359,7 @@ rtError pxStorePNGImage(const char *filename, pxOffscreen &b, bool /*grayscale*/
* conjunction with the documentation file libjpeg.txt.
*
* This code will not do anything useful as-is, but it may be helpful as a
* skeleton for constructing routines that call the JPEG library.
* skeleton for constructing routines that call the JPEG library.
*
* We present these routines in the same coding style used in the JPEG code
* (ANSI function definitions, etc); but you are of course free to code your
Expand Down Expand Up @@ -708,8 +708,23 @@ rtError pxLoadJPGImageTurbo(const char *buf, size_t buflen, pxOffscreen &o)
return RT_FAIL;// TODO : add grayscale support for libjpeg turbo. falling back to libjpeg for now
}

// limit memory usage to resolution 4096x4096
if (((size_t)width * height) > ((size_t)4096 * 4096))
{
rtLogError("Error libjpeg-turbo: image too large");
tjDestroy(jpegDecompressor);
return RT_FAIL;
}

unsigned char *imageBuffer = tjAlloc(width * height * 3);

if (!imageBuffer)
{
rtLogError("Error allocating libjpeg-turbo buffer");
tjDestroy(jpegDecompressor);
return RT_FAIL;
}

int result = tjDecompress2(jpegDecompressor, (unsigned char *)buf, buflen, imageBuffer, width, 0, height, TJPF_RGB /*(colorComponent == 3) ? TJPF_RGB : jpegColorspace*/, TJFLAG_FASTDCT);

if (result != 0)
Expand Down Expand Up @@ -1151,9 +1166,9 @@ rtError pxLoadAPNGImage(const char *imageData, size_t imageDataSize,

//unsigned int width, height, channels, rowbytes, size, i, j;
unsigned int width, height, i, j;

unsigned long size, rowbytes;

png_bytepp rows_image;
png_bytepp rows_frame;
unsigned char *p_image;
Expand Down