Skip to content
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
3 changes: 2 additions & 1 deletion lib/jpegli/decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@ void jpegli_crop_scanline(j_decompress_ptr cinfo, JDIMENSION* xoffset,
JPEGLI_ERROR("Output cropping is not supported in raw data mode");
}
if (xoffset == nullptr || width == nullptr || *width == 0 ||
*xoffset + *width > cinfo->output_width) {
*xoffset >= cinfo->output_width ||
*width > cinfo->output_width - *xoffset) {
JPEGLI_ERROR("jpegli_crop_scanline: Invalid arguments");
}
// TODO(szabadka) Skip the IDCT for skipped over blocks.
Expand Down
19 changes: 19 additions & 0 deletions lib/jpegli/error_handling_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,25 @@ TEST(DecoderErrorHandlingTest, NoReadScanlines) {
jpegli_destroy_decompress(&cinfo);
}

TEST(DecoderErrorHandlingTest, CropScanlineArgumentOverflow) {
jpeg_decompress_struct cinfo = {};
const auto try_catch_block = [&]() -> bool {
ERROR_HANDLER_SETUP(jpegli);
jpegli_create_decompress(&cinfo);
jpegli_mem_src(&cinfo, kCompressed0, kLen0);
jpegli_read_header(&cinfo, TRUE);
jpegli_start_decompress(&cinfo);
// xoffset + width wraps around JDIMENSION and lands below output_width, so
// an unguarded sum accepts this out-of-range crop window.
JDIMENSION xoffset = 0xffffffffu;
JDIMENSION width = 2;
jpegli_crop_scanline(&cinfo, &xoffset, &width);
return true;
};
EXPECT_FALSE(try_catch_block());
jpegli_destroy_decompress(&cinfo);
}

const size_t kMaxImageWidth = 0xffff;
JSAMPLE kOutputBuffer[MAX_COMPONENTS * kMaxImageWidth];

Expand Down