Skip to content

codecs/bmp: harden decoder against panics on sub-byte indexed color and zero dimensions - #3124

Open
ByGh00st wants to merge 2 commits into
image-rs:mainfrom
ByGh00st:fix/bmp-subbyte-indexed-panic
Open

ByGh00st wants to merge 2 commits into
image-rs:mainfrom
ByGh00st:fix/bmp-subbyte-indexed-panic

Conversation

@ByGh00st

@ByGh00st ByGh00st commented Oct 13, 2026 •

Copy link
Copy Markdown

Summary

Hardens BmpDecoder against runtime panics by:

  1. Rejecting non-8-bit palettized formats when indexed color decoding is requested.
  2. Enforcing strictly positive image dimensions (width > 0 and height != 0).

Details

  • Indexed Color Panics: When set_indexed_color(true) was enabled on sub-byte palettized formats (1-bit, 2-bit, or 4-bit BMP), row.clone_from_slice(&indices[0..width]) panicked with an out-of-bounds slice access. The decoder now returns ImageError::Unsupported.
  • Zero Dimensions Panics: Images specifying height == 0 or width <= 0 were previously accepted during header parsing. Downstream row arithmetic ((height - 1).try_into().unwrap()) resulted in a TryFromIntError unwrap panic. Headers with non-positive dimensions are now rejected cleanly during header validation.

Testing

  • Added regression test test_indexed_color_sub_byte_rejected
  • Added regression test test_bmp_zero_height_rejected
  • All 31 BMP codec unit tests pass cleanly.

Backward Compatibility

  • Zero breaking changes to public APIs.
  • Valid BMP images decode identically.

@ByGh00st
ByGh00st force-pushed the fix/bmp-subbyte-indexed-panic branch from 57d8ffd to c9b2348 Compare October 13, 2026 13:55
@ByGh00st ByGh00st changed the title codecs/bmp: reject sub-byte indexed color to prevent out-of-bounds slice panic codecs/bmp: harden decoder against panics on sub-byte indexed color and zero dimensions Oct 13, 2026

This branch has not been deployed

No deployments
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