Skip to content

Commit 021e5db

Browse files
bpo-36946:Fix possible signed integer overflow when handling slices. (GH-15639)
This is a complement to PR 13375. (cherry picked from commit 3c87a66) Co-authored-by: HongWeipeng <[email protected]>
1 parent 30933d5 commit 021e5db

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

Lib/test/test_list.py

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def test_reversed_pickle(self):
149149
a[:] = data
150150
self.assertEqual(list(it), [])
151151

152+
def test_step_overflow(self):
153+
a = [0, 1, 2, 3, 4]
154+
a[1::sys.maxsize] = [0]
155+
self.assertEqual(a[3::sys.maxsize], [3])
156+
152157
def test_no_comdat_folding(self):
153158
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
154159
# optimization causes failures in code that relies on distinct
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -5019,7 +5019,8 @@ Pointer_subscript(PyObject *myself, PyObject *item)
50195019
PyObject *np;
50205020
StgDictObject *stgdict, *itemdict;
50215021
PyObject *proto;
5022-
Py_ssize_t i, len, cur;
5022+
Py_ssize_t i, len;
5023+
size_t cur;
50235024

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

Objects/listobject.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -2729,7 +2729,8 @@ list_subscript(PyListObject* self, PyObject* item)
27292729
return list_item(self, i);
27302730
}
27312731
else if (PySlice_Check(item)) {
2732-
Py_ssize_t start, stop, step, slicelength, cur, i;
2732+
Py_ssize_t start, stop, step, slicelength, i;
2733+
size_t cur;
27332734
PyObject* result;
27342735
PyObject* it;
27352736
PyObject **src, **dest;
@@ -2865,7 +2866,8 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28652866
/* assign slice */
28662867
PyObject *ins, *seq;
28672868
PyObject **garbage, **seqitems, **selfitems;
2868-
Py_ssize_t cur, i;
2869+
Py_ssize_t i;
2870+
size_t cur;
28692871

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

0 commit comments

Comments
 (0)