]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
In PySlice_IndicesEx, clip the step to [-PY_SSIZE_T_MAX, PY_SSIZE_T_MAX] rather than...
authorMark Dickinson <dickinsm@gmail.com>
Fri, 6 Aug 2010 18:55:26 +0000 (18:55 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Fri, 6 Aug 2010 18:55:26 +0000 (18:55 +0000)
Misc/NEWS
Objects/sliceobject.c

index 0567a20d03812cc5d90f033220b6998d87558f8c..bac8d001d23713aa4e1bdbb4bb3d349ba0b02bb5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -529,6 +529,10 @@ Core and Builtins
 C-API
 -----
 
+- PySlice_GetIndicesEx now clips the step to [-PY_SSIZE_T_MAX, PY_SSIZE_T_MAX]
+  instead of [-PY_SSIZE_T_MAX-1, PY_SSIZE_T_MAX].  This makes it safe to do
+  "step = -step" when reversing a slice.
+
 - Issue #5753: A new C API function, `PySys_SetArgvEx`, allows embedders of the
   interpreter to set sys.argv without also modifying sys.path.  This helps fix
   `CVE-2008-5983
index ee89006688d3787e9c78a0d81900b3d50682cffc..55fda52987fd788bf2f4fb7063d107ad860fb9c3 100644 (file)
@@ -131,7 +131,8 @@ PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
 
 int
 PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
-                     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
+                     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
+                     Py_ssize_t *slicelength)
 {
     /* this is harder to get right than you might think */
 
@@ -147,6 +148,13 @@ PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
                             "slice step cannot be zero");
             return -1;
         }
+        /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it
+         * with -PY_SSIZE_T_MAX.  This doesn't affect the semantics, and it
+         * guards against later undefined behaviour resulting from code that
+         * does "step = -step" as part of a slice reversal.
+         */
+        if (*step < -PY_SSIZE_T_MAX)
+            *step = -PY_SSIZE_T_MAX;
     }
 
     defstart = *step < 0 ? length-1 : 0;