Skip to content

Never crash: an i16::MIN offset, a GPS position that isn't one, overflowing gain-map dimensions and a PSD block over 4 GiB no longer overflow or write garbage - #1851

Merged
emmertex merged 5 commits into
emmertex:stagingfrom
blwfish:fix/low-never-crash-meta-codecs
Dec 2, 2026

Conversation

@blwfish

@blwfish blwfish commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

In short: four overflow or garbage-in cases in the Exif, gain-map and PSD writers. Each gets a checked operation and a test at its boundary.

Change

  • Exif UTC offset: offset_text used o.abs() on an i16; i16::MIN overflows (a panic in a debug build, a wrong sign in release). It uses unsigned_abs now.
  • GPS: write_exif wrote latitude and longitude without a finite or range check. It writes no GPS block when either is non-finite or beyond ±90 / ±180, which is what read_gps already refuses; the edge values themselves are still written. A non-finite altitude alone is left out of an otherwise good block.
  • Gain map: encode_gain and apply multiplied input-derived dimensions unchecked; GainImage::has_samples uses checked_mul and the encoder returns an error. The same overflow in encode_with_gain (u32 width x height x channels on 64-bit) is fixed too, with its 65,535 limit check moved ahead of the product.
  • PSD: each resource length is narrowed with u32::try_from (resource_len); a metadata block over 4 GiB is an encode error instead of a truncated length. resource_section returns a Result.
  • No manual or release-note change.

How it was checked

Each fails with its fix reverted: an i16::MIN, -1, 0, 1, i16::MAX offset sweep; a NaN position that wrote a 0,0 position; a multiply-overflow gain map; resource_len at u32::MAX, +1, +2 and usize::MAX (no 4 GiB allocation). The encode_with_gain change is covered by the boundary tests of the gain map, but its own revert wasn't checked separately. cargo test -p lcc-meta -p lcc-codecs passes.

  • cargo xtask ci --quick --base origin/staging on this branch (macOS): all steps pass, including the tests of lcc-codecs and lcc-meta, and parity, layers, assets, wasm and deny.

🤖 Generated with Claude Code

blwfish and others added 5 commits December 2, 2026 13:01
…an encode error

A GainImage whose width x height x channels overflows usize (the fields are
public, and a base image of u32::MAX x u32::MAX x 4 does the same in
encode_with_gain) was multiplied unchecked before the size cap was applied:
a panic in debug builds, a wrapped and so wrongly passing size check in
release. The products are now checked_mul'd (one helper for the gain map's
sample count), the 65535 cap runs first in encode_with_gain, and an overflow
is an Error::Encode, with nothing allocated. apply() uses the same check.

Test: overflowing_dimensions_are_refused_without_allocating (fails without the
fix with "attempt to multiply with overflow").

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…a truncated length

Each image resource's length was written as `data.len() as u32`: an ICC, EXIF
or XMP block longer than u32::MAX wrote a wrapped length and a corrupt file.
resource_section now returns a Result and the length goes through
u32::try_from (resource_len), failing with an actionable "too large for PSD"
encode error.

Test: resource_length_is_checked_not_truncated pins u32::MAX (fits), +1, +2 and
usize::MAX (errors); fails without the fix.

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…Exif

write_exif turned latitude and longitude into degrees/minutes/seconds with
`as u32` and no check: NaN wrote 0 deg, infinity or 1e300 wrote u32::MAX
degrees, and a latitude past 90 or a longitude past 180 wrote a block the
reader refuses. A NaN altitude was written as 0 m. Now a position whose
latitude or longitude is not finite or is off the globe (the same bounds the
reader applies) gets no GPS block, and a non-finite altitude is left out of an
otherwise good block. The edges (+-90, +-180) are still written.

Test: gps_that_is_not_a_position_is_not_written covers the edges, one step past
each, NaN, +-inf, f64::MAX and non-finite altitudes; it fails without the fix.

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lows when written to Exif

The OffsetTime text used `o.abs()` on the i16 offset: for i16::MIN that
overflows (a panic in debug builds, a wrapped negative magnitude in release).
The text now comes from offset_text, which takes the unsigned magnitude as
DateTime::to_iso already does. A hostile offset (a file's OffsetTimeOriginal
or a caller-set value) therefore writes a refusable but harmless text.

Test: offset_text_survives_the_extreme_offsets pins i16::MIN, -1, 0, 1 and
i16::MAX and writes a capture time with each; fails without the fix
("attempt to negate with overflow").

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@emmertex
emmertex enabled auto-merge December 2, 2026 19:06
@emmertex
emmertex merged commit 1d4a98c into emmertex:staging Dec 2, 2026
3 checks passed
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.

2 participants