Skip to content

Commit e041df7

Browse files
author
Matt Wozniski
committed
Add tests for overflows converting ints to bytes
1 parent 9ce042a commit e041df7

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

tests/basics/array_overflow.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import skip_if
2+
skip_if.no_bigint()
3+
4+
try:
5+
from array import array
6+
except ImportError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
def test_array_overflow(typecode, val):
11+
try:
12+
print(array(typecode, [val]))
13+
except OverflowError:
14+
print('OverflowError')
15+
16+
# small int -1
17+
test_array_overflow('Q', -1)
18+
test_array_overflow('L', -1)
19+
test_array_overflow('I', -1)
20+
test_array_overflow('H', -1)
21+
test_array_overflow('B', -1)
22+
23+
# big int -1
24+
test_array_overflow('Q', -2**64 // 2**64)
25+
test_array_overflow('L', -2**64 // 2**64)
26+
test_array_overflow('I', -2**64 // 2**64)
27+
test_array_overflow('H', -2**64 // 2**64)
28+
test_array_overflow('B', -2**64 // 2**64)
29+
30+
# big int 2**63
31+
test_array_overflow('q', 2**63)
32+
test_array_overflow('l', 2**63)
33+
test_array_overflow('i', 2**63)
34+
test_array_overflow('h', 2**63)
35+
test_array_overflow('b', 2**63)

tests/basics/int_bytes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@
1717
(1).to_bytes(-1, "little")
1818
except ValueError:
1919
print("ValueError")
20+
21+
# too small buffer should raise an error
22+
try:
23+
(256).to_bytes(1, "little")
24+
except OverflowError:
25+
print("OverflowError")
26+
27+
# negative numbers should raise an error
28+
try:
29+
(-256).to_bytes(2, "little")
30+
except OverflowError:
31+
print("OverflowError")

tests/basics/int_bytes_intbig.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@
1515

1616
# check that extra zero bytes don't change the internal int value
1717
print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little"))
18+
19+
# too small buffer should raise an error
20+
try:
21+
(2**64).to_bytes(8, "little")
22+
except OverflowError:
23+
print("OverflowError")
24+
25+
# negative numbers should raise an error
26+
try:
27+
(-2**64).to_bytes(9, "little")
28+
except OverflowError:
29+
print("OverflowError")

tests/basics/struct_overflow.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import skip_if
2+
skip_if.no_bigint()
3+
4+
try:
5+
import ustruct as struct
6+
except:
7+
try:
8+
import struct
9+
except ImportError:
10+
print("SKIP")
11+
raise SystemExit
12+
13+
def test_struct_overflow(typecode, val):
14+
try:
15+
print(struct.pack(typecode, val))
16+
except OverflowError:
17+
print('OverflowError')
18+
except struct.error:
19+
print('OverflowError')
20+
21+
# small int -1
22+
test_struct_overflow('>Q', -1)
23+
test_struct_overflow('>L', -1)
24+
test_struct_overflow('>I', -1)
25+
test_struct_overflow('>H', -1)
26+
test_struct_overflow('>B', -1)
27+
28+
# big int -1
29+
test_struct_overflow('>Q', -2**64 // 2**64)
30+
test_struct_overflow('>L', -2**64 // 2**64)
31+
test_struct_overflow('>I', -2**64 // 2**64)
32+
test_struct_overflow('>H', -2**64 // 2**64)
33+
test_struct_overflow('>B', -2**64 // 2**64)
34+
35+
# possibly small ints
36+
test_struct_overflow('>q', 2**63)
37+
test_struct_overflow('>l', 2**31)
38+
test_struct_overflow('>i', 2**31)
39+
test_struct_overflow('>h', 2**15)
40+
test_struct_overflow('>b', 2**7)
41+
42+
# definitely big ints
43+
test_struct_overflow('>q', 2**64 // 2**1)
44+
test_struct_overflow('>l', 2**64 // 2**33)
45+
test_struct_overflow('>i', 2**64 // 2**33)
46+
test_struct_overflow('>h', 2**64 // 2**49)
47+
test_struct_overflow('>b', 2**64 // 2**57)

0 commit comments

Comments
 (0)