Skip to content

Guard displayExponent overflow in _toScientific (#185)#245

Open
thedavidmeister wants to merge 4 commits into
mainfrom
2026-06-17-issue-185-scientific-exponent-overflow
Open

Guard displayExponent overflow in _toScientific (#185)#245
thedavidmeister wants to merge 4 commits into
mainfrom
2026-06-17-issue-185-scientific-exponent-overflow

Conversation

@thedavidmeister

@thedavidmeister thedavidmeister commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • _toScientific computes displayExponent = exponent + scaleExponent where scaleExponent is 75 or 76 (from maximizeFull). For valid Float values whose exponent is within ~76 of int32.max, displayExponent overflows int32 and the formatter emits a string the parser cannot re-pack — a silent data-corruption bug.
  • Fix: guard the overflow and revert with UnformatableExponent(exponent) before emitting.
  • Two new tests: testFormatScientificDisplayExponentOverflowReverts (exact reproduction of the bug with (10, int32.max)) and testFormatScientificExponentAtMaxBoundarySucceeds (boundary: (1, int32.max) formats cleanly to "1e2147483647").

Closes #185

⚠️ REQUIRES REDEPLOY BEFORE MERGE

This changes LibFormatDecimalFloat, which is part of the deployed DecimalFloat contract. The testDeployAddress and testExpectedCodeHashDecimalFloat CI tests will fail until the Manual sol artifacts workflow is triggered on this branch:

gh workflow run manual-sol-artifacts.yaml --ref 2026-06-17-issue-185-scientific-exponent-overflow -f suite=decimal-float

Test plan

  • testFormatScientificDisplayExponentOverflowReverts — new, confirms revert for the overflow case
  • testFormatScientificExponentAtMaxBoundarySucceeds — new, confirms the boundary value still formats
  • All 25 existing LibFormatDecimalFloatToDecimalStringTest tests still pass
  • Mutation-kill verified: testFormatScientificDisplayExponentOverflowReverts uses vm.expectRevert — removing the guard causes the test to fail

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Added test coverage for scientific format exponent boundary handling.

After maximizeFull, scaleExponent is 75 or 76. For Floats with large
positive exponents (within ~76 of int32.max), displayExponent =
exponent + scaleExponent exceeds int32.max, producing a formatted string
whose exponent the parser cannot re-pack into int32. Revert with
UnformatableExponent rather than silently emitting an un-parseable string.

The negative-overflow case is also guarded but unreachable in practice:
the minimum post-maximizeFull exponent from a valid Float is int32.min - 76,
giving displayExponent = int32.min exactly (still in range).

Two new tests pin the fix: one asserting the revert for (10, int32.max)
and one confirming (1, int32.max) still formats successfully.

Bytecode changes - requires manual-sol-artifacts redeploy before merge.

Co-Authored-By: Claude <noreply@anthropic.com>
@thedavidmeister thedavidmeister self-assigned this Jun 17, 2026
Update DecimalFloat artifact hash after displayExponent overflow guard.
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 462cfb5a-cf10-4ad9-8342-10587057956e

📥 Commits

Reviewing files that changed from the base of the PR and between dabe1cd and 4558d2b.

📒 Files selected for processing (1)
  • test/src/lib/format/LibFormatDecimalFloat.toDecimalString.t.sol

Walkthrough

Two unit tests are added to LibFormatDecimalFloatToDecimalStringTest to cover the displayExponent int32 overflow fix from issue #185. One test asserts a revert with UnformatableExponent when the display exponent exceeds int32 range; the other asserts successful formatting at the exact int32.max boundary.

Changes

Scientific displayExponent overflow tests

Layer / File(s) Summary
Scientific displayExponent overflow and boundary tests
test/src/lib/format/LibFormatDecimalFloat.toDecimalString.t.sol
testFormatScientificDisplayExponentOverflowReverts constructs a lossless Float that overflows the internal displayExponent range and asserts revert with UnformatableExponent. testFormatScientificExponentAtMaxBoundarySucceeds asserts that formatting 1 with int32.max exponent succeeds and returns the canonical 1e<maxInt32> string.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: guarding against displayExponent overflow in _toScientific per issue #185.
Linked Issues check ✅ Passed The PR fully implements approach (1) from issue #185: detecting displayExponent overflow and reverting with UnformatableExponent, with two comprehensive tests validating overflow and boundary cases.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to adding tests for the displayExponent overflow fix; no unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 2026-06-17-issue-185-scientific-exponent-overflow

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

thedavidmeister and others added 2 commits June 21, 2026 01:11
Accept main's originalExponent approach in UnformatableExponent revert;
update test to expect originalExponent (user-visible) instead of post-maximizeFull exponent.

Co-Authored-By: Claude <noreply@anthropic.com>
@thedavidmeister

Copy link
Copy Markdown
Contributor Author

Producer note: WAITING-DEPLOY (+1 inherited fuzz red). The 5 testProdDeployment* pins fail ('DecimalFloat not deployed') — same condition as main; needs the pre-merge deploy, not a code fix. The additional testRoundTripFuzzPow failure is the pre-existing #239 pow exponent-overflow bug hit by this run's fuzz seed — this PR only touches the _toScientific display path; the failure disappears once the #239 fix (#244 or #242) merges into this branch.

@thedavidmeister

Copy link
Copy Markdown
Contributor Author

🤖 ai:producer
Producer note: CORRECTION — my previous note said this PR carries '+1 inherited fuzz red' (testRoundTripFuzzPow). That detail was wrong: it belongs to PR #244's run, not this one. This PR's current run (28592793150) fails ONLY the 5 testProdDeployment* 'DecimalFloat not deployed' pins (508 passed), identical to main (run 28391168330). State remains WAITING-DEPLOY: greens at the interactive pre-merge deploy from this branch.

@thedavidmeister

Copy link
Copy Markdown
Contributor Author

🤖 ai:producer
Producer note: CORRECTION of my 2026-07-05 correction — it inverted reality. Re-read of run 28592793150 (this PR's current head 0dfd28c): it has SIX failing tests — the 5 testProdDeployment* prod pins (WAITING-DEPLOY, same as main) PLUS testRoundTripFuzzPow in LibDecimalFloat.pow.t.sol (fuzz counterexample hitting the pre-existing #239 pow exponent-overflow bug; this PR's diff is test-only in format tests and cannot cause a pow revert — the fix carriers are #242/#244). It was PR #244's run that shows only the 5 pins. State: WAITING-DEPLOY + 1 inherited fuzz red; no code action here.

@thedavidmeister thedavidmeister added the ai:ready AI vetter: passes review, ready for human decision label Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ready AI vetter: passes review, ready for human decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scientific format near int32.max exponent produces un-parseable display exponent

1 participant