Fix %d/%o/%x overflow for large doubles#605
Open
JoshRosen wants to merge 2 commits intodatabricks:masterfrom
Open
Fix %d/%o/%x overflow for large doubles#605JoshRosen wants to merge 2 commits intodatabricks:masterfrom
JoshRosen wants to merge 2 commits intodatabricks:masterfrom
Conversation
%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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes an overflow bug when large doubles are used in jsonnet string formatting:
%d,%o, and%xformat specs usedDouble.toLongto convert to an integer, which silently clamps toLong.MAX_VALUE(~9.2e18) for large double values like1e19or1e20truncateToIntegerhelper (mirroring the idiom inRenderUtils.renderDouble) that usesLongas a fast path and falls back toBigDecimal.toBigIntfor values exceedingLongrangeBigInt, so edge cases like-0.3(which truncates to0) are handled naturally.Test plan
%d,%o,%xwith values exceeding Long range (1e19,1e20,-1e19)-0.3,-0.9) that truncate to zeroformat.jsonnettest suite covers format strings that produce outputs with leading negative signs.All code written by Claude Opus 4.6.