Skip to content

Never crash: the deep watermark clips to its buffer, a rating above five is clamped like the sidecar's, and unserialisable settings are left out of a sidecar - #1850

Merged
emmertex merged 4 commits into
emmertex:stagingfrom
blwfish:fix/low-never-crash-engine
Dec 2, 2026

Conversation

@blwfish

@blwfish blwfish commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

In short: three places in export where a number or a value from a stored library could be turned into something wrong or a panic-shaped slice, each now bounded and pinned by a test.

Change

  • Deep watermark: the 16-bit and float paths took &mut v[i..i + 3] from a sample buffer whose length they never checked; a buffer shorter than the image (a short decode) would panic. They now use get_mut(i..i + 3) with saturating index math and clip to the samples there are.
  • Rating: export metadata and the sidecar packet narrowed the rating with photo.rating as i8. A stored rating of 6 to 127 was written as that many stars and 128 to 255 went negative, which the sidecar reader takes for Reject. A new sidecar::file_rating (min(5)) is the one narrowing both use.
  • Settings JSON: two places unwrap_or_default() the develop settings serialisation, so a failure would write an empty lc:settings. Both go through a settings_json helper that logs a warning and leaves lc:settings out instead. Valid settings behave as before; a merge into an existing sidecar replaces the old lc:* settings as it did with the empty string. Serialising the settings practically cannot fail today (NaN becomes null), so this is hardening.
  • No manual or release-note change: the only visible effect is for stored ratings above 127, which used to read back as Reject.

How it was checked

deep_watermark_survives_a_sample_buffer_shorter_than_its_size (buffer lengths 0, 1, 2, 3, half, half+1, full-2, full-1 and full, for 16-bit and float and the alpha path), a_rating_above_five_is_written_as_five_stars (0, 1, 4, 5, 6, 127, 128, 200, 255; reads the sidecar packet back: rating five, no Reject), settings_that_cant_be_serialised_are_left_out_not_written_empty. Each fails with the old code put back (the first panics with "range start index 43698 out of range for slice of length 0"). 359 engine export, sidecar and metadata tests pass.

  • cargo xtask ci --quick --base origin/staging on this branch (macOS): fmt, brand, quarantine, docs, notes, i18n, windows, clippy and stamp pass. Tests of lcc-engine: all pass except device_sync::tests::a_shared_collection_brings_photos_files_and_edits_both_ways (environmental on that machine; it also fails on plain staging there). Parity, layers, assets, wasm and deny: pass.

🤖 Generated with Claude Code

blwfish and others added 4 commits December 2, 2026 13:11
A high-bit-depth image whose sample buffer is shorter than
width x height x 3 (a hand-built or truncated DeepImage; the fields are
public) made the 16-bit and float watermark paths index `v[i + c]` past the
end and panic. Both paths now take the pixel as `get_mut(i..i + 3)` with
saturating index math and skip pixels the buffer does not have.

Test: deep_watermark_survives_a_sample_buffer_shorter_than_its_size marks
buffers of length 0, 1, 2, 3, half, half+1, full-2, full-1 and full for both
sample types (and through the alpha path); it panics with the old indexing.

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A stored rating above 5 was narrowed with `as i8` in the export metadata:
6 to 127 were written as that many stars and 128 to 255 went negative,
which a sidecar reader takes for Reject (the sidecar writer already clamped
with `.min(5)`). One `sidecar::file_rating` now narrows a rating for the
sidecar packet and the exported file alike: 0 writes nothing to a file, 1 to
5 are kept, anything larger is five.

Test: a_rating_above_five_is_written_as_five_stars covers 0, 1, 4, 5, 6, 127,
128, 200 and 255 through export_metadata, the XMP it writes, and the export
sidecar packet read back (rating and no Reject); it fails at 6 with the old
cast.

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…f a sidecar, not written empty

The sidecar packet turned a failed serialisation of the develop settings
(a value with no JSON form) into an empty string with `unwrap_or_default()`
and wrote it as `lc:settings`, which every reader takes for "no settings"
with no word anywhere. Both sites (settings with and without a plug-in
filter stack) now go through `settings_json`: a failure is logged and the
packet carries no `lc:settings`. Settings that serialise (non-finite numbers
become JSON null) are written as before.

Test: settings_that_cant_be_serialised_are_left_out_not_written_empty feeds a
failing Serialize and a pair-keyed map (None), a NaN/inf list (written as
null) and the photo's real settings (still in the packet); the first two
return Some("") with the old fallback.

Manual: none, no user-visible change

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The new deep-watermark test passes the floor scale that staging's
draw_watermark_deep_over now takes (emmertex#1848).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emmertex
emmertex enabled auto-merge December 2, 2026 19:53
@emmertex

emmertex commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Merged staging into this branch: #1848 (now on staging) added a floor_scale argument to draw_watermark_deep_over, so the new deep_watermark_survives_a_sample_buffer_shorter_than_its_size test now passes 1.0 for it (it would not have compiled otherwise). cargo xtask ci --quick is green locally; auto-merge is on.

@emmertex
emmertex merged commit 1f0e069 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