Skip to content

Reject mismatched frame size in libjpeg encoder - #249

Draft
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:jpegli-jpg-frame-size-check
Draft

Reject mismatched frame size in libjpeg encoder#249
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:jpegli-jpg-frame-size-check

Conversation

@rootkiller6788

Copy link
Copy Markdown

What

The extras JPEG encoder (lib/extras/enc/jpg.cc) encodes frames with the loop for (size_t y = 0; y < info.ysize; ++y) reading pixels + y * image.stride, and per-pixel for (size_t x = 0; x < info.xsize; ++x) reads. The only gate before this is Encoder::VerifyPackedImageEncoder::VerifyImageSize, whose image.xsize != info.xsize || image.ysize != info.ysize check is commented out (see the TODO at lib/extras/enc/encode.cc).

When a PackedPixelFile carries info.xsize/info.ysize larger than the frame's color.xsize/color.ysize, the encoder reads past the pixel buffer — the same out-of-bounds read that PR #213 fixed for the jpegli encoder (lib/extras/enc/jpegli.cc), which this encoder path was missing.

Why it matters

Encoder::FromExtension(".jpg") is the public route used by djpegli (-o out.jpg) and by the extras encoder framework. An inconsistent PackedPixelFile currently leads to a heap-buffer-overflow read (crash under ASan/MSan) instead of a clean error.

Fix

Add an explicit frame-size check in JPEGEncoder::Encode, returning JPEGLI_FAILURE("Frame size does not match image size.") on mismatch, plus a regression test JpegliLibjpegEncodeRejectsMismatchedFrameSize that mirrors the existing JpegliEncodeRejectsMismatchedFrameSize test for the jpegli encoder.

Verified against real code: EncodeWithLibJpeg/EncodeWithSJpeg iterate info.ysize/info.xsize over the frame buffer, and no other guard rejects the mismatch.

The extras JPEG encoder reads ppf.info.ysize rows and ppf.info.xsize
columns from the frame's pixel buffer, but the shared VerifyImageSize
check for frame size matching the image size is commented out. A
PackedPixelFile whose info dimensions are larger than the frame buffer
causes an out-of-bounds read in the encoder copy loops.

Add an explicit frame size check in JPEGEncoder::Encode and a test that
mirrors the existing jpegli encoder mismatch test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant