Repository navigation
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 intoDec 2, 2026
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
offset_textusedo.abs()on ani16;i16::MINoverflows (a panic in a debug build, a wrong sign in release). It usesunsigned_absnow.write_exifwrote 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 whatread_gpsalready refuses; the edge values themselves are still written. A non-finite altitude alone is left out of an otherwise good block.encode_gainandapplymultiplied input-derived dimensions unchecked;GainImage::has_samplesuseschecked_muland the encoder returns an error. The same overflow inencode_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.u32::try_from(resource_len); a metadata block over 4 GiB is an encode error instead of a truncated length.resource_sectionreturns aResult.How it was checked
Each fails with its fix reverted: an
i16::MIN, -1, 0, 1,i16::MAXoffset sweep; a NaN position that wrote a 0,0 position; a multiply-overflow gain map;resource_lenatu32::MAX, +1, +2 andusize::MAX(no 4 GiB allocation). Theencode_with_gainchange 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-codecspasses.cargo xtask ci --quick --base origin/stagingon 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