Skip to content

fix: avoid panic in snapshot summary update_totals on malformed values#161

Merged
vovacf201 merged 2 commits into
dev_rebase_main_20260303from
fix/update-totals-overflow
May 29, 2026
Merged

fix: avoid panic in snapshot summary update_totals on malformed values#161
vovacf201 merged 2 commits into
dev_rebase_main_20260303from
fix/update-totals-overflow

Conversation

@vovacf201

@vovacf201 vovacf201 commented May 22, 2026

Copy link
Copy Markdown
Collaborator

What changes are included in this PR?

Fixes a panic in iceberg::spec::snapshot_summary::update_totals that aborts any writer attempting to commit against a table whose snapshot summary contains a value that does not parse cleanly into a u64.
The function previously did:
.map_or(0, |value| value.parse::().unwrap()) // previous total
.map(|value| value.parse::().unwrap()) // added / removed
new_total -= value; // unchecked subtraction
Three latent panics in one function:

  1. PosOverflow — a previous summary value greater than u64::MAX (e.g. written by a non-Rust engine using unbounded integers, or otherwise corrupt history) yields ParseIntError { kind: PosOverflow } and aborts the writer.
  2. InvalidDigit — a non-numeric or empty string aborts the same way.
  3. Underflow — new_total -= value panics in debug builds and silently wraps in release when removed > previous + added, which is reachable on corrupt history.
    This PR replaces all three with parse-or-zero (unwrap_or(0)) plus saturating_add / saturating_sub, factored through a small local parse_or_zero helper. Semantics now match Java Iceberg's SnapshotSummary, which treats missing or unparseable values as 0. A single corrupt prior summary can no longer indefinitely break commits for a table.
    No public API change; update_totals is a private free function and its caller (update_snapshot_summaries) is unchanged.

Are these changes tested?

Yes — three new unit tests in crates/iceberg/src/spec/snapshot_summary.rs::tests, plus the five existing tests in the same module continue to pass:

test_update_totals_handles_overflowing_previous_total — seeds total-files-size with "99999999999999999999" (> u64::MAX). Previously panicked; now resets the overflowing total to 0 and applies added cleanly.

test_update_totals_saturates_on_underflow — removed=100, previous=5, added=1 previously panicked on new_total -= value; now saturates to 0.

test_update_totals_treats_malformed_value_as_zero — "garbage" and "" are accepted as 0 instead of panicking.
cargo test -p iceberg --lib spec::snapshot_summary::

test result: ok. 8 passed; 0 failed; 0 ignored
cargo fmt and cargo clippy -p iceberg --lib --tests --no-deps are clean on the touched file.

Previously update_totals called .unwrap() when parsing snapshot summary
properties (total-*, added-*, removed-*) as u64. A value written by a
non-Rust writer that exceeds u64::MAX (or is otherwise malformed) caused
ParseIntError { kind: PosOverflow } and aborted the writer. The same
function used checked subtraction (`new_total -= value`), which could
panic in debug builds on a removed > previous + added skew from corrupt
history.

Replace both with parse-or-zero + saturating arithmetic so a single
corrupt prior summary cannot indefinitely break compaction / commit for
a table. Behavior matches Java Iceberg's SnapshotSummary, which treats
missing or unparseable values as 0.

Add tests for the three failure modes (overflowing previous total,
removed > previous + added, non-numeric value).
@chenzl25

Copy link
Copy Markdown
Collaborator

Thanks for the fix. One concern: update_snapshot_summaries() can still panic on truncate overwrite, because truncate_table_summary() still parses previous summary values via get_prop() and then unwraps. Could we apply the same parse-or-zero / non-panicking behavior there as well, and add a malformed/overflow truncate-overwrite test?

…values

`update_snapshot_summaries` routes the truncate-overwrite path through
`truncate_table_summary`, which parsed previous-summary totals via
`get_prop` as `i32` and propagated `ParseIntError` with `?`. The caller
then did `.map_err(...).unwrap()`, so a malformed, empty, or
u64-overflowing previous total (legal in summaries written by other
engines) would panic the writer. The `i32` ceiling was also too narrow
for real-world `TOTAL_FILE_SIZE` values (~2.1 GB), making the panic
reachable on perfectly valid input.

Apply the same parse-or-zero / non-panicking policy already used in
`update_totals`:

- Replace `get_prop` with `previous_total_or_zero`, which parses as
  `u64` and returns 0 for missing, malformed, or overflowing values.
- Make `truncate_table_summary` infallible and collapse the six
  near-identical blocks into a single (total, removed) loop.
- Drop the `.map_err(...).unwrap()` in `update_snapshot_summaries`.

Add a test that drives the full truncate-overwrite path with a previous
summary containing garbage, empty, u64-overflowing, i32-overflowing
(but valid u64), well-formed, and missing totals. Asserts no panic,
all totals reset to 0, malformed/missing previous totals do not emit a
bogus removed_* counter, and well-formed totals (including the value
that would have broken the old i32 parser) still flow into removed_*.
@vovacf201

Copy link
Copy Markdown
Collaborator Author

@chenzl25 it should be fixed now, thanks

@chenzl25 chenzl25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM

@vovacf201 vovacf201 merged commit 4822c11 into dev_rebase_main_20260303 May 29, 2026
17 checks passed
@vovacf201 vovacf201 deleted the fix/update-totals-overflow branch May 29, 2026 09:37
amitgilad3 pushed a commit to lakeops-org/iceberg-rust-risingwave that referenced this pull request Jun 4, 2026
risingwavelabs#161)

* fix: avoid panic in snapshot summary update_totals on malformed values

Previously update_totals called .unwrap() when parsing snapshot summary
properties (total-*, added-*, removed-*) as u64. A value written by a
non-Rust writer that exceeds u64::MAX (or is otherwise malformed) caused
ParseIntError { kind: PosOverflow } and aborted the writer. The same
function used checked subtraction (`new_total -= value`), which could
panic in debug builds on a removed > previous + added skew from corrupt
history.

Replace both with parse-or-zero + saturating arithmetic so a single
corrupt prior summary cannot indefinitely break compaction / commit for
a table. Behavior matches Java Iceberg's SnapshotSummary, which treats
missing or unparseable values as 0.

Add tests for the three failure modes (overflowing previous total,
removed > previous + added, non-numeric value).

* fix: avoid panic in snapshot summary truncate overwrite on malformed values

`update_snapshot_summaries` routes the truncate-overwrite path through
`truncate_table_summary`, which parsed previous-summary totals via
`get_prop` as `i32` and propagated `ParseIntError` with `?`. The caller
then did `.map_err(...).unwrap()`, so a malformed, empty, or
u64-overflowing previous total (legal in summaries written by other
engines) would panic the writer. The `i32` ceiling was also too narrow
for real-world `TOTAL_FILE_SIZE` values (~2.1 GB), making the panic
reachable on perfectly valid input.

Apply the same parse-or-zero / non-panicking policy already used in
`update_totals`:

- Replace `get_prop` with `previous_total_or_zero`, which parses as
  `u64` and returns 0 for missing, malformed, or overflowing values.
- Make `truncate_table_summary` infallible and collapse the six
  near-identical blocks into a single (total, removed) loop.
- Drop the `.map_err(...).unwrap()` in `update_snapshot_summaries`.

Add a test that drives the full truncate-overwrite path with a previous
summary containing garbage, empty, u64-overflowing, i32-overflowing
(but valid u64), well-formed, and missing totals. Asserts no panic,
all totals reset to 0, malformed/missing previous totals do not emit a
bogus removed_* counter, and well-formed totals (including the value
that would have broken the old i32 parser) still flow into removed_*.
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