Skip to content

Commit 300ac8d

Browse files
committed
[GR-67327] Add missing check for negative value to int.__lshift__
PullRequest: graalpython/3896
2 parents 9053479 + 991035a commit 300ac8d

File tree

2 files changed

+35
-3
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints

2 files changed

+35
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@ def test_bigint():
109109
assert isinstance(i, int)
110110
assert i == BIG_NUMBER
111111

112+
def test_bigint_lshift():
113+
INT_SHIFT = -2147483648
114+
LONG_SHIFT = -2147483649
115+
116+
assert_raises(ValueError, lambda: 0 << INT_SHIFT)
117+
assert_raises(ValueError, lambda: -1 << INT_SHIFT)
118+
assert_raises(ValueError, lambda: ((1 << 32) - 1) << INT_SHIFT)
119+
assert_raises(ValueError, lambda: ((1 << 64) - 1) << INT_SHIFT)
120+
assert_raises(ValueError, lambda: BIG_NUMBER << INT_SHIFT)
121+
122+
assert_raises(ValueError, lambda: 0 << LONG_SHIFT)
123+
assert_raises(ValueError, lambda: -1 << LONG_SHIFT)
124+
assert_raises(ValueError, lambda: 2147483647 << LONG_SHIFT)
125+
assert_raises(ValueError, lambda: 9223372036854775807 << LONG_SHIFT)
126+
assert_raises(ValueError, lambda: BIG_NUMBER << LONG_SHIFT)
127+
128+
def test_bigint_rshift():
129+
INT_SHIFT = -2147483648
130+
LONG_SHIFT = -2147483649
131+
132+
assert_raises(ValueError, lambda: 0 >> INT_SHIFT)
133+
assert_raises(ValueError, lambda: -1 >> INT_SHIFT)
134+
assert_raises(ValueError, lambda: ((1 >> 32) - 1) >> INT_SHIFT)
135+
assert_raises(ValueError, lambda: ((1 >> 64) - 1) >> INT_SHIFT)
136+
assert_raises(ValueError, lambda: BIG_NUMBER >> INT_SHIFT)
137+
138+
assert_raises(ValueError, lambda: 0 >> LONG_SHIFT)
139+
assert_raises(ValueError, lambda: -1 >> LONG_SHIFT)
140+
assert_raises(ValueError, lambda: 2147483647 >> LONG_SHIFT)
141+
assert_raises(ValueError, lambda: 9223372036854775807 >> LONG_SHIFT)
142+
assert_raises(ValueError, lambda: BIG_NUMBER >> LONG_SHIFT)
112143

113144
def test_boolean2int():
114145
assert int(True) == 1
@@ -425,10 +456,10 @@ def test_create_int_from_float():
425456
assert True
426457
else:
427458
assert False, "expected ValueError"
428-
459+
429460
class FloatSub(float):
430461
pass
431-
462+
432463
try:
433464
int(FloatSub(float('nan')))
434465
except ValueError:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,9 +1852,10 @@ static PInt doGuardedBiI(Node inliningTarget, BigInteger left, int right, PRaise
18521852
static PInt doPiL(PInt left, long right,
18531853
@Bind Node inliningTarget,
18541854
@Shared @Cached PRaiseNode raiseNode) {
1855+
raiseNegativeShiftCount(inliningTarget, right < 0, raiseNode);
18551856
int rightI = (int) right;
18561857
if (rightI == right) {
1857-
return doPiI(left, rightI, inliningTarget, raiseNode);
1858+
return doGuardedBiI(inliningTarget, left.getValue(), rightI, raiseNode);
18581859
} else {
18591860
throw raiseNode.raise(inliningTarget, PythonErrorType.OverflowError);
18601861
}

0 commit comments

Comments
 (0)