From 6bc5917903b722bdd0e5d3020949f26fec5dfe9a Mon Sep 17 00:00:00 2001 From: Alexey Izbyshev Date: Thu, 23 May 2019 03:01:08 +0300 Subject: [PATCH] =?UTF-8?q?bpo-35091:=20Objects/listobject.c:=20Replace=20?= =?UTF-8?q?overflow=20checks=20in=20gallop=20fu=E2=80=A6=20(GH-10202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …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 --- Objects/listobject.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 08b3e89a957..3185957453d 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1380,9 +1380,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; @@ -1403,9 +1402,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; @@ -1471,9 +1469,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; @@ -1495,9 +1492,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;