bpo-30281: Fix the default value for stop in PySlice_Unpack() (#1531)… · python/cpython@05469fa (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -137,6 +137,8 @@ _PySlice_Unpack(PyObject *_r,
137 137 PySliceObject *r = (PySliceObject *)_r;
138 138 /* this is harder to get right than you might think */
139 139
140 +assert(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX);
141 +
140 142 if (r->step == Py_None) {
141 143 *step = 1;
142 144 }
@@ -157,14 +159,14 @@ _PySlice_Unpack(PyObject *_r,
157 159 }
158 160
159 161 if (r->start == Py_None) {
160 -*start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;;
162 +*start = *step < 0 ? PY_SSIZE_T_MAX : 0;
161 163 }
162 164 else {
163 165 if (!_PyEval_SliceIndex(r->start, start)) return -1;
164 166 }
165 167
166 168 if (r->stop == Py_None) {
167 -*stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX;
169 +*stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX;
168 170 }
169 171 else {
170 172 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
@@ -198,7 +200,7 @@ _PySlice_AdjustIndices(Py_ssize_t length,
198 200 *stop = (step < 0) ? -1 : 0;
199 201 }
200 202 }
201 -else if (*stop >= length) {
203 +else if (*stop >= length) {
202 204 *stop = (step < 0) ? length - 1 : length;
203 205 }
204 206
Original file line number Diff line number Diff line change
@@ -4679,7 +4679,7 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4679 4679 /* Extract a slice index from a PyInt or PyLong or an object with the
4680 4680 nb_index slot defined, and store in *pi.
4681 4681 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4682 - and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4682 + and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
4683 4683 Return 0 on error, 1 on success.
4684 4684 */
4685 4685 /* Note: If v is NULL, return success without storing into *pi. This