Skip to content

Reject truncated JPEG image buffers - #38

Merged
bghira merged 1 commit into
mainfrom
fix/reject-truncated-jpegs
Aug 10, 2026
Merged

Reject truncated JPEG image buffers#38
bghira merged 1 commit into
mainfrom
fix/reject-truncated-jpegs

Conversation

@bghira

@bghira bghira commented Aug 10, 2026

Copy link
Copy Markdown
Owner

This pull request improves the robustness of JPEG decoding in the imdecode function by adding explicit validation to detect truncated or malformed JPEG files before attempting to decode them. It also introduces comprehensive tests to ensure truncated or manipulated JPEGs are correctly rejected and that trailing bytes after a valid JPEG are tolerated.

JPEG validation improvements:

  • Added a validate_jpeg_complete function to the imdecode implementation in cv_compat.rs that parses JPEG markers to ensure the image is complete and not truncated before decoding. This function checks for the presence and correct order of JPEG markers, including the end-of-image marker, and rejects files with missing or misplaced markers. [1] [2]

Testing enhancements:

  • Added new Rust tests in tests.rs to verify that truncated JPEGs are rejected, JPEGs with marker bytes in metadata are not incorrectly accepted as complete, and valid JPEGs with trailing non-image data are still accepted. [1] [2]
  • Added a Python binding test to ensure that truncated JPEGs are rejected by the Python interface as well, raising an appropriate error.

@bghira
bghira merged commit 174c623 into main Aug 10, 2026
10 checks passed
@bghira
bghira deleted the fix/reject-truncated-jpegs branch August 10, 2026 19:42
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