Skip to content

fix(core): promote to BigInt instead of panicking on neg/abs of i64::MIN 💥#172

Merged
timfennis merged 1 commit into
masterfrom
bugfix/neg-abs-i64-min-overflow
Jun 2, 2026
Merged

fix(core): promote to BigInt instead of panicking on neg/abs of i64::MIN 💥#172
timfennis merged 1 commit into
masterfrom
bugfix/neg-abs-i64-min-overflow

Conversation

@timfennis
Copy link
Copy Markdown
Owner

Context

Continuing the recent sweep of panic-on-edge-case fixes (negative power, windows size 0, integer division by zero), I went looking for another arithmetic input that crashes the interpreter. Negating or taking the absolute value of i64::MIN does it: the positive magnitude 2^63 does not fit in an i64.

abs(-9223372036854775807 - 1)   // i64::MIN

Int::abs and Int::neg called i.abs() / i.neg() directly on the Int64 variant. Both overflow at i64::MIN, panicking with "attempt to negate with overflow" in debug builds and silently wrapping to a negative result in release.

Changes

  • ndc_core/src/int.rs: Int::abs and Int::neg now use checked_abs / checked_neg and promote to BigInt on overflow, matching the checked-then-promote pattern already used by the binary operators.
  • Regression tests: 001_math/032_neg_abs_i64_min.ndc and 900_bugs/bug0026_neg_abs_i64_min_overflow.ndc.

Every other path produces the same results as before; only the i64::MIN boundary changes from a panic to the correct 9223372036854775808.

🤖

…MIN 💥

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timfennis timfennis force-pushed the bugfix/neg-abs-i64-min-overflow branch from 6594f37 to 0ff827e Compare June 2, 2026 06:11
@timfennis timfennis enabled auto-merge (squash) June 2, 2026 06:12
@timfennis timfennis merged commit 331a89b into master Jun 2, 2026
1 check passed
@timfennis timfennis deleted the bugfix/neg-abs-i64-min-overflow branch June 2, 2026 06:13
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