topdown: fix format_int precision loss for integers larger than 64 bits#8857
Open
Synvoya wants to merge 2 commits into
Open
topdown: fix format_int precision loss for integers larger than 64 bits#8857Synvoya wants to merge 2 commits into
format_int precision loss for integers larger than 64 bits#8857Synvoya wants to merge 2 commits into
Conversation
format_int routed integers through a 64-bit-mantissa big.Float, corrupting any value above ~2^64 (disagreeing with sprintf). Format integer inputs through an exact big.Int; fractional inputs keep float truncation. Signed-off-by: Synvoya <16019863+Synvoya@users.noreply.github.com>
WASM's format_int cannot represent integers larger than 64 bits (get uint failed), the same limitation already tracked for integer literals (open-policy-agent#3711). Add the new golden case to the WASM e2e exceptions so it runs under Go (where the fix applies) but is skipped under WASM. Signed-off-by: Synvoya <16019863+Synvoya@users.noreply.github.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.
Description
format_int(x, base)corrupts integers that need more than 64 bits of precision, in every base. It routes the value throughbuiltins.NumberToFloat(abig.Floatwith a 64-bit mantissa) thenf.Int(), so any integer above ~2^64 is rounded before formatting:sprintf("%x", [18446744073709551617])returns the correct10000000000000001, so two builtins disagree on the same exact-integer value.Fix
Format integer inputs through an exact
big.Int(mirroringbuiltinSprintf). Fractional/exponent inputs still fall through to the existing float-truncation path, soformat_int(15.9, 16) == "f"andformat_int(-15.9, 16) == "-f"are unchanged.Test
Added a golden case covering a >2^64 integer in bases 2/8/10/16, negatives, and the fractional-truncation cases. Full
go test ./v1/topdown/passes (900+ existing string golden cases, no regressions).