Skip to content

Fix %d/%o/%x overflow for large doubles#605

Open
JoshRosen wants to merge 2 commits intodatabricks:masterfrom
JoshRosen:fix-format-integer-overflow
Open

Fix %d/%o/%x overflow for large doubles#605
JoshRosen wants to merge 2 commits intodatabricks:masterfrom
JoshRosen:fix-format-integer-overflow

Conversation

@JoshRosen
Copy link
Contributor

This PR fixes an overflow bug when large doubles are used in jsonnet string formatting:

  • %d, %o, and %x format specs used Double.toLong to convert to an integer, which silently clamps to Long.MAX_VALUE (~9.2e18) for large double values like 1e19 or 1e20
  • Add a truncateToInteger helper (mirroring the idiom in RenderUtils.renderDouble) that uses Long as a fast path and falls back to BigDecimal.toBigInt for values exceeding Long range
  • Sign and digits both derive from the truncated BigInt, so edge cases like -0.3 (which truncates to 0) are handled naturally.

Test plan

  • Added unit tests for %d, %o, %x with values exceeding Long range (1e19, 1e20, -1e19)
  • Added edge cases for fractional negatives (-0.3, -0.9) that truncate to zero
  • Expected values verified against official C and Go jsonnet implementations
  • Existing format.jsonnet test suite covers format strings that produce outputs with leading negative signs.

All code written by Claude Opus 4.6.

JoshRosen and others added 2 commits February 17, 2026 23:29
%d, %o, %x format specs silently overflow to Long.MAX_VALUE for doubles
exceeding Long range (~9.2e18). Also add edge cases for sign determination
when fractional negatives like -0.3 truncate to zero.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Double.toLong silently overflows to Long.MAX_VALUE for values exceeding
~9.2e18. Add a truncateToInteger helper (mirroring RenderUtils.renderDouble)
that uses Long as a fast path and falls back to BigDecimal.toBigInt. Sign
and digits both derive from the truncated BigInt, so fractional negatives
like -0.3 that truncate to zero naturally produce no minus sign.

Co-Authored-By: Claude Opus 4.6 <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.

1 participant