Skip to content

Commit 05469fa

Browse files
authored
bpo-30281: Fix the default value for stop in PySlice_Unpack() (#1531) (#1480)
1 parent 8832141 commit 05469fa

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Objects/sliceobject.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ _PySlice_Unpack(PyObject *_r,
137137
PySliceObject *r = (PySliceObject *)_r;
138138
/* this is harder to get right than you might think */
139139

140+
assert(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX);
141+
140142
if (r->step == Py_None) {
141143
*step = 1;
142144
}
@@ -157,14 +159,14 @@ _PySlice_Unpack(PyObject *_r,
157159
}
158160

159161
if (r->start == Py_None) {
160-
*start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;;
162+
*start = *step < 0 ? PY_SSIZE_T_MAX : 0;
161163
}
162164
else {
163165
if (!_PyEval_SliceIndex(r->start, start)) return -1;
164166
}
165167

166168
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;
168170
}
169171
else {
170172
if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
@@ -198,7 +200,7 @@ _PySlice_AdjustIndices(Py_ssize_t length,
198200
*stop = (step < 0) ? -1 : 0;
199201
}
200202
}
201-
else if (*stop >= length) {
203+
else if (*stop >= length) {
202204
*stop = (step < 0) ? length - 1 : length;
203205
}
204206

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4679,7 +4679,7 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
46794679
/* Extract a slice index from a PyInt or PyLong or an object with the
46804680
nb_index slot defined, and store in *pi.
46814681
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.
46834683
Return 0 on error, 1 on success.
46844684
*/
46854685
/* Note: If v is NULL, return success without storing into *pi. This

0 commit comments

Comments
 (0)