Kaydet (Commit) 05469fa1 authored tarafından Xiang Zhang's avatar Xiang Zhang Kaydeden (comit) GitHub

bpo-30281: Fix the default value for stop in PySlice_Unpack() (#1531) (#1480)

üst 88321413
...@@ -137,6 +137,8 @@ _PySlice_Unpack(PyObject *_r, ...@@ -137,6 +137,8 @@ _PySlice_Unpack(PyObject *_r,
PySliceObject *r = (PySliceObject *)_r; PySliceObject *r = (PySliceObject *)_r;
/* this is harder to get right than you might think */ /* this is harder to get right than you might think */
assert(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX);
if (r->step == Py_None) { if (r->step == Py_None) {
*step = 1; *step = 1;
} }
...@@ -157,14 +159,14 @@ _PySlice_Unpack(PyObject *_r, ...@@ -157,14 +159,14 @@ _PySlice_Unpack(PyObject *_r,
} }
if (r->start == Py_None) { if (r->start == Py_None) {
*start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;; *start = *step < 0 ? PY_SSIZE_T_MAX : 0;
} }
else { else {
if (!_PyEval_SliceIndex(r->start, start)) return -1; if (!_PyEval_SliceIndex(r->start, start)) return -1;
} }
if (r->stop == Py_None) { if (r->stop == Py_None) {
*stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX; *stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX;
} }
else { else {
if (!_PyEval_SliceIndex(r->stop, stop)) return -1; if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
...@@ -198,7 +200,7 @@ _PySlice_AdjustIndices(Py_ssize_t length, ...@@ -198,7 +200,7 @@ _PySlice_AdjustIndices(Py_ssize_t length,
*stop = (step < 0) ? -1 : 0; *stop = (step < 0) ? -1 : 0;
} }
} }
else if (*stop >= length) { else if (*stop >= length) {
*stop = (step < 0) ? length - 1 : length; *stop = (step < 0) ? length - 1 : length;
} }
......
...@@ -4679,7 +4679,7 @@ ext_call_fail: ...@@ -4679,7 +4679,7 @@ ext_call_fail:
/* Extract a slice index from a PyInt or PyLong or an object with the /* Extract a slice index from a PyInt or PyLong or an object with the
nb_index slot defined, and store in *pi. nb_index slot defined, and store in *pi.
Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, 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. Return 0 on error, 1 on success.
*/ */
/* Note: If v is NULL, return success without storing into *pi. This /* Note: If v is NULL, return success without storing into *pi. This
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment