Skip to content

Commit

Permalink
bpo-35091: Objects/listobject.c: Replace overflow checks in gallop fu… (
Browse files Browse the repository at this point in the history
GH-10202)

…nctions with asserts

The actual overflow can never happen because of the following:
* The size of a list can't be greater than PY_SSIZE_T_MAX / sizeof(PyObject*).
* The size of a pointer on all supported plaftorms is at least 4 bytes.
* ofs is positive and less than the list size at the beginning of each iteration.

https://bugs.python.org/issue35091
(cherry picked from commit 6bc5917)

Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
  • Loading branch information
miss-islington and izbyshev committed May 23, 2019
1 parent b73c21c commit 367fe57
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,9 +1324,8 @@ gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_
while (ofs < maxofs) {
IFLT(a[ofs], key) {
lastofs = ofs;
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
ofs = (ofs << 1) + 1;
if (ofs <= 0) /* int overflow */
ofs = maxofs;
}
else /* key <= a[hint + ofs] */
break;
Expand All @@ -1347,9 +1346,8 @@ gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_
break;
/* key <= a[hint - ofs] */
lastofs = ofs;
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
ofs = (ofs << 1) + 1;
if (ofs <= 0) /* int overflow */
ofs = maxofs;
}
if (ofs > maxofs)
ofs = maxofs;
Expand Down Expand Up @@ -1415,9 +1413,8 @@ gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize
while (ofs < maxofs) {
IFLT(key, *(a-ofs)) {
lastofs = ofs;
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
ofs = (ofs << 1) + 1;
if (ofs <= 0) /* int overflow */
ofs = maxofs;
}
else /* a[hint - ofs] <= key */
break;
Expand All @@ -1439,9 +1436,8 @@ gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize
break;
/* a[hint + ofs] <= key */
lastofs = ofs;
assert(ofs <= (PY_SSIZE_T_MAX - 1) / 2);
ofs = (ofs << 1) + 1;
if (ofs <= 0) /* int overflow */
ofs = maxofs;
}
if (ofs > maxofs)
ofs = maxofs;
Expand Down

0 comments on commit 367fe57

Please sign in to comment.