fix: << overflow check for large shift amounts (multiples of 64)#969
Open
He-Pin wants to merge 1 commit into
Open
fix: << overflow check for large shift amounts (multiples of 64)#969He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
Motivation: The << operator falsely errors when the shift amount is a multiple of 64 (e.g. 1 << 64, 0 << 128). Root cause: Java's << masks shift count to 6 bits, so 1L << (63 - 64) becomes 1L << 63 = Long.MIN_VALUE (negative), making math.abs(ll) >= Long.MIN_VALUE always true — a false overflow. Modification: Mask shift amount to rr % 64 before applying the overflow check, in both tryInlineArith and visitBinaryOp in Evaluator.scala. This matches the semantics of go-jsonnet and jrsonnet. Result: Expressions like 1 << 64, 0 << 128, 1 << 192 now return correct results instead of false errors. Genuine overflows like 3 << 126 still error correctly. | Expression | Before | After | go-jsonnet | jrsonnet | |-----------|---------|-------|------------|----------| | 1 << 64 | ERROR | 1 | 1 | 1 | | 0 << 128 | ERROR | 0 | 0 | 0 | | 1 << 192 | ERROR | 1 | 1 | 1 | | 3 << 126 | error | error | wraps | error | | 4.5 << 66 | 16 | 16 | 16 | 16 |
adf07c6 to
3e84390
Compare
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.
Summary
Fix
<<operator falsely error when shift amount is a multiple of 64.Motivation
The
<<operator in sjsonnet incorrectly throws an overflow error when the shift amount is a multiple of 64 (e.g.,1 << 64,0 << 128,1 << 192). Both go-jsonnet and jrsonnet handle these cases correctly by masking the shift amount modulo 64 (matching Java's native<<behavior onlong).Root cause: The overflow check
math.abs(ll) >= (1L << (63 - rr))breaks whenrr >= 64andrr % 64 == 0. Java's<<masks the shift count to 6 bits, so1L << (63 - 64)becomes1L << 63 = Long.MIN_VALUE(negative). Sincemath.abs(ll) >= Long.MIN_VALUEis always true, a false overflow error is raised.Modification
Mask the shift amount to
rr % 64before applying the overflow check, in bothtryInlineArithandvisitBinaryOpinEvaluator.scala. This matches the semantics of go-jsonnet and jrsonnet.Result
Expressions like
1 << 64,0 << 128,1 << 192now return correct results instead of false errors. Genuine overflows like3 << 126still error correctly.Behavior Comparison
1 << 640 << 640 << 10001 << 1281 << 1924.5 << 663 << 1261 << 63Test plan
1 << 64returns 1 (was false error)0 << 64returns 0 (was false error)0 << 1000returns 0 (was false error)1 << 128returns 1 (was false error)1 << 192returns 1 (was false error)4.5 << 66returns 163 << 126still correctly errors on overflow