Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6fc740

Browse files
committed
In PySlice_IndicesEx, clip the step to [-PY_SSIZE_T_MAX, PY_SSIZE_T_MAX] rather than [PY_SSIZE_T_MIN, PY_SSIZE_T_MAX].
1 parent af5ac39 commit e6fc740

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ Core and Builtins
529529
C-API
530530
-----
531531

532+
- PySlice_GetIndicesEx now clips the step to [-PY_SSIZE_T_MAX, PY_SSIZE_T_MAX]
533+
instead of [-PY_SSIZE_T_MAX-1, PY_SSIZE_T_MAX]. This makes it safe to do
534+
"step = -step" when reversing a slice.
535+
532536
- Issue #5753: A new C API function, `PySys_SetArgvEx`, allows embedders of the
533537
interpreter to set sys.argv without also modifying sys.path. This helps fix
534538
`CVE-2008-5983

Objects/sliceobject.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
131131

132132
int
133133
PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
134-
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
134+
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
135+
Py_ssize_t *slicelength)
135136
{
136137
/* this is harder to get right than you might think */
137138

@@ -147,6 +148,13 @@ PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
147148
"slice step cannot be zero");
148149
return -1;
149150
}
151+
/* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it
152+
* with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it
153+
* guards against later undefined behaviour resulting from code that
154+
* does "step = -step" as part of a slice reversal.
155+
*/
156+
if (*step < -PY_SSIZE_T_MAX)
157+
*step = -PY_SSIZE_T_MAX;
150158
}
151159

152160
defstart = *step < 0 ? length-1 : 0;

0 commit comments

Comments
 (0)