Skip to content

Guard displayExponent overflow in _toScientific (#185)#245

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

Guard displayExponent overflow in _toScientific (#185)#245
thedavidmeister wants to merge 3 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.

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>
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.

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

1 participant