From 9ca77e9eb2d384eda2e517e9bdf8aeb239fefeb4 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Fri, 5 May 2017 18:48:52 +0800 Subject: [PATCH 1/3] bpo-30281: fix the default value for stop in PySlice_Unpack() --- Objects/sliceobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index ebc44642fea423..1ea69db2c6beb1 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -217,14 +217,14 @@ PySlice_Unpack(PyObject *_r, } if (r->start == Py_None) { - *start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;; + *start = *step < 0 ? PY_SSIZE_T_MAX : 0; } else { if (!_PyEval_SliceIndex(r->start, start)) return -1; } if (r->stop == Py_None) { - *stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX; + *stop = *step < 0 ? -PY_SSIZE_T_MAX-1 : PY_SSIZE_T_MAX; } else { if (!_PyEval_SliceIndex(r->stop, stop)) return -1; @@ -258,7 +258,7 @@ PySlice_AdjustIndices(Py_ssize_t length, *stop = (step < 0) ? -1 : 0; } } - else if (*stop >= length) { + else if (*stop >= length) { *stop = (step < 0) ? length - 1 : length; } From 83e2f40c5df53ea403410096366df1e6084b0e0b Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Sat, 6 May 2017 09:51:50 +0800 Subject: [PATCH 2/3] use PY_SSIZE_T_MIN instead of -PY_SSIZE_T_MAX-1 --- Objects/sliceobject.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 1ea69db2c6beb1..4263737e4143be 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -197,6 +197,8 @@ PySlice_Unpack(PyObject *_r, PySliceObject *r = (PySliceObject*)_r; /* this is harder to get right than you might think */ + Py_BUILD_ASSERT(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX); + if (r->step == Py_None) { *step = 1; } @@ -224,7 +226,7 @@ PySlice_Unpack(PyObject *_r, } if (r->stop == Py_None) { - *stop = *step < 0 ? -PY_SSIZE_T_MAX-1 : PY_SSIZE_T_MAX; + *stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX; } else { if (!_PyEval_SliceIndex(r->stop, stop)) return -1; From b9301346ca30364c291becd3e47bac431c09b57b Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Wed, 10 May 2017 17:54:00 +0800 Subject: [PATCH 3/3] update doc and comment --- Doc/c-api/slice.rst | 2 +- Python/ceval.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/slice.rst b/Doc/c-api/slice.rst index aece011a994d96..f8395ec8d77022 100644 --- a/Doc/c-api/slice.rst +++ b/Doc/c-api/slice.rst @@ -75,7 +75,7 @@ Slice Objects Extract the start, stop and step data members from a slice object as C integers. Silently reduce values larger than ``PY_SSIZE_T_MAX`` to ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than - ``-PY_SSIZE_T_MAX-1`` to ``-PY_SSIZE_T_MAX-1``, and silently boost the step + ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``. Return ``-1`` on error, ``0`` on success. diff --git a/Python/ceval.c b/Python/ceval.c index afd305cf9af44e..23fd088098165a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4889,7 +4889,7 @@ do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) /* Extract a slice index from a PyLong or an object with the nb_index slot defined, and store in *pi. Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, - and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. + and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN. Return 0 on error, 1 on success. */ int