Skip to content

Commit 3c87a66

Browse files
hongweipengserhiy-storchaka
authored andcommitted
bpo-36946:Fix possible signed integer overflow when handling slices. (GH-15639)
This is a complement to PR 13375.
1 parent 32a960f commit 3c87a66

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

Lib/test/test_list.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ def test_reversed_pickle(self):
150150
a[:] = data
151151
self.assertEqual(list(it), [])
152152

153+
def test_step_overflow(self):
154+
a = [0, 1, 2, 3, 4]
155+
a[1::sys.maxsize] = [0]
156+
self.assertEqual(a[3::sys.maxsize], [3])
157+
153158
def test_no_comdat_folding(self):
154159
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
155160
# optimization causes failures in code that relies on distinct
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix possible signed integer overflow when handling slices. Patch by hongweipeng.

Modules/_ctypes/_ctypes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5195,7 +5195,8 @@ Pointer_subscript(PyObject *myself, PyObject *item)
51955195
PyObject *np;
51965196
StgDictObject *stgdict, *itemdict;
51975197
PyObject *proto;
5198-
Py_ssize_t i, len, cur;
5198+
Py_ssize_t i, len;
5199+
size_t cur;
51995200

52005201
/* Since pointers have no length, and we want to apply
52015202
different semantics to negative indices than normal

Objects/listobject.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,7 +2789,8 @@ list_subscript(PyListObject* self, PyObject* item)
27892789
return list_item(self, i);
27902790
}
27912791
else if (PySlice_Check(item)) {
2792-
Py_ssize_t start, stop, step, slicelength, cur, i;
2792+
Py_ssize_t start, stop, step, slicelength, i;
2793+
size_t cur;
27932794
PyObject* result;
27942795
PyObject* it;
27952796
PyObject **src, **dest;
@@ -2925,7 +2926,8 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29252926
/* assign slice */
29262927
PyObject *ins, *seq;
29272928
PyObject **garbage, **seqitems, **selfitems;
2928-
Py_ssize_t cur, i;
2929+
Py_ssize_t i;
2930+
size_t cur;
29292931

29302932
/* protect against a[::-1] = a */
29312933
if (self == (PyListObject*)value) {

0 commit comments

Comments
 (0)