From a13b109bc00487182b13c1d02c03e8910dfb9234 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sun, 15 Apr 2012 16:30:35 +0100 Subject: Issue 13496: Fix bisect.bisect overflow bug for large collections. --- Modules/_bisectmodule.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'Modules/_bisectmodule.c') diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index 7fecfc6937..93d0eed41e 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -21,7 +21,10 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t return -1; } while (lo < hi) { - mid = (lo + hi) / 2; + /* The (size_t)cast ensures that the addition and subsequent division + are performed as unsigned operations, avoiding difficulties from + signed overflow. (See issue 13496.) */ + mid = ((size_t)lo + hi) / 2; litem = PySequence_GetItem(list, mid); if (litem == NULL) return -1; @@ -121,7 +124,10 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h return -1; } while (lo < hi) { - mid = (lo + hi) / 2; + /* The (size_t)cast ensures that the addition and subsequent division + are performed as unsigned operations, avoiding difficulties from + signed overflow. (See issue 13496.) */ + mid = ((size_t)lo + hi) / 2; litem = PySequence_GetItem(list, mid); if (litem == NULL) return -1; -- cgit v1.2.1