Skip to content

Commit 6a3dc43

Browse files
committed
Fix: undetected overfow in int.to_bytes
1 parent b15be29 commit 6a3dc43

File tree

2 files changed

+6
-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

+6
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def checkPIntSpec(self, tests, byteorder, signed=False):
639639
.format(test, byteorder, signed)) from err
640640

641641

642-
def test_SignedBitEndian(self):
642+
def test_SignedBigEndian(self):
643643
# Convert integers to signed big-endian byte arrays.
644644
tests1 = {
645645
0: b'\x00',
@@ -699,7 +699,7 @@ def test_UnsignedBigEndian(self):
699699
32767: b'\x7f\xff',
700700
32768: b'\x80\x00',
701701
65535: b'\xff\xff',
702-
65536: b'\x01\x00\x00'
702+
65536: b'\x01\x00\x00',
703703
}
704704
self.check(tests3, 'big', signed=False)
705705
self.checkPIntSpec(tests3, 'big', signed=False)
@@ -736,6 +736,8 @@ def test_WrongInput(self):
736736
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False)
737737
self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
738738
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')
739+
self.assertRaises(OverflowError, (4294967296).to_bytes, 4, 'big')
740+
self.assertRaises(OverflowError, (4294967296).to_bytes, 4, 'little')
739741

740742
def test_WrongTypes(self):
741743
class MyTest():

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
@@ -2131,10 +2131,11 @@ public static byte[] fromLong(long self, int byteCount, boolean isBigEndian, boo
21312131
number >>= 8;
21322132
index += delta;
21332133
}
2134-
if (overflowProfile.profile((number != 0 && bytes.length == 1 && bytes[0] != self) || (signed && bytes.length == 1 && bytes[0] != self) || (byteCount == 0 && self != 0))) {
21352134

2135+
if (overflowProfile.profile(!signed && number != 0 || (signed && bytes.length == 1 && bytes[0] != self) || (byteCount == 0 && self != 0))) {
21362136
throw raise.raise(PythonErrorType.OverflowError, MESSAGE_INT_TO_BIG);
21372137
}
2138+
21382139
if (signed) {
21392140
while (0 <= index && index <= (byteCount - 1)) {
21402141
bytes[index] = signByte;

0 commit comments

Comments
 (0)