summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2019-05-17 07:13:03 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2019-05-17 07:13:03 (GMT)
commit14514d9084a40f599c57da853a305aa264562a43 (patch)
tree36cb61b74dbfda9ce1cdf72a864b640d0bda546d /Lib
parent870b035bc6da96689b59dd6f79782ec6f1873617 (diff)
downloadcpython-14514d9084a40f599c57da853a305aa264562a43.zip
cpython-14514d9084a40f599c57da853a305aa264562a43.tar.gz
cpython-14514d9084a40f599c57da853a305aa264562a43.tar.bz2
bpo-36946: Fix possible signed integer overflow when handling slices. (GH-13375)
The final addition (cur += step) may overflow, so use size_t for "cur". "cur" is always positive (even for negative steps), so it is safe to use size_t here. Co-Authored-By: Martin Panter <vadmium+py@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/test_arrays.py11
-rw-r--r--Lib/test/seq_tests.py1
-rw-r--r--Lib/test/string_tests.py2
-rw-r--r--Lib/test/test_array.py4
-rw-r--r--Lib/test/test_bytes.py5
-rw-r--r--Lib/test/test_mmap.py4
6 files changed, 20 insertions, 7 deletions
diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py
index 0fc5d7e..87ecbf0 100644
--- a/Lib/ctypes/test/test_arrays.py
+++ b/Lib/ctypes/test/test_arrays.py
@@ -69,6 +69,17 @@ class ArrayTestCase(unittest.TestCase):
from operator import delitem
self.assertRaises(TypeError, delitem, ca, 0)
+ def test_step_overflow(self):
+ a = (c_int * 5)()
+ a[3::sys.maxsize] = (1,)
+ self.assertListEqual(a[3::sys.maxsize], [1])
+ a = (c_char * 5)()
+ a[3::sys.maxsize] = b"A"
+ self.assertEqual(a[3::sys.maxsize], b"A")
+ a = (c_wchar * 5)()
+ a[3::sys.maxsize] = u"X"
+ self.assertEqual(a[3::sys.maxsize], u"X")
+
def test_numeric_arrays(self):
alen = 5
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py
index 6aedd2b..65b110e 100644
--- a/Lib/test/seq_tests.py
+++ b/Lib/test/seq_tests.py
@@ -209,6 +209,7 @@ class CommonTest(unittest.TestCase):
a = self.type2test([0,1,2,3,4])
self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2]))
self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4]))
+ self.assertEqual(a[3::sys.maxsize], self.type2test([3]))
def test_contains(self):
u = self.type2test([0, 1, 2])
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 836a43b..38da941 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -1135,7 +1135,7 @@ class MixinStrUnicodeUserStringTest:
def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.
s = string.ascii_letters + string.digits
- indices = (0, None, 1, 3, 41, -1, -2, -37)
+ indices = (0, None, 1, 3, 41, sys.maxsize, -1, -2, -37)
for start in indices:
for stop in indices:
# Skip step 0 (invalid)
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 57c396d..c243957 100644
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -746,7 +746,7 @@ class BaseTest:
# Test extended slicing by comparing with list slicing
# (Assumes list conversion works correctly, too)
a = array.array(self.typecode, self.example)
- indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
+ indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100)
for start in indices:
for stop in indices:
# Everything except the initial 0 (invalid step)
@@ -844,7 +844,7 @@ class BaseTest:
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)
def test_extended_set_del_slice(self):
- indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
+ indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100)
for start in indices:
for stop in indices:
# Everything except the initial 0 (invalid step)
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index a091360..9502a8f 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -285,7 +285,7 @@ class BaseBytesTest:
# Test extended slicing by comparing with list slicing.
L = list(range(255))
b = self.type2test(L)
- indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
+ indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100)
for start in indices:
for stop in indices:
# Skip step 0 (invalid)
@@ -1242,7 +1242,8 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
self.assertLessEqual(sys.getsizeof(b), size)
def test_extended_set_del_slice(self):
- indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
+ indices = (0, None, 1, 3, 19, 300, 1<<333, sys.maxsize,
+ -1, -2, -31, -300)
for start in indices:
for stop in indices:
# Skip invalid step 0
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index dd857a0..495d24a 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -439,7 +439,7 @@ class MmapTests(unittest.TestCase):
m = mmap.mmap(-1, len(s))
m[:] = s
self.assertEqual(m[:], s)
- indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
+ indices = (0, None, 1, 3, 19, 300, sys.maxsize, -1, -2, -31, -300)
for start in indices:
for stop in indices:
# Skip step 0 (invalid)
@@ -451,7 +451,7 @@ class MmapTests(unittest.TestCase):
# Test extended slicing by comparing with list slicing.
s = bytes(reversed(range(256)))
m = mmap.mmap(-1, len(s))
- indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
+ indices = (0, None, 1, 3, 19, 300, sys.maxsize, -1, -2, -31, -300)
for start in indices:
for stop in indices:
# Skip invalid step 0