diff options
author | Mark Dickinson <mdickinson@enthought.com> | 2012-04-15 15:43:19 (GMT) |
---|---|---|
committer | Mark Dickinson <mdickinson@enthought.com> | 2012-04-15 15:43:19 (GMT) |
commit | 0407e960610f043f3459cdc224300dddf7941765 (patch) | |
tree | 5c4d03098565599cf5bc2eac0dfb409fb7012cfc /Modules/_bisectmodule.c | |
parent | 9c0baf72020d8ab452101dd7c447ec61e16a96d6 (diff) | |
download | cpython-0407e960610f043f3459cdc224300dddf7941765.zip cpython-0407e960610f043f3459cdc224300dddf7941765.tar.gz cpython-0407e960610f043f3459cdc224300dddf7941765.tar.bz2 |
Issue 13496: Fix bisect.bisect overflow bug for large collections.
Diffstat (limited to 'Modules/_bisectmodule.c')
-rw-r--r-- | Modules/_bisectmodule.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index e8976b2..4798c12 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -21,7 +21,13 @@ 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.) */ + printf("lo: %d\n", lo); + printf("hi: %d\n", hi); + printf("mid: %d\n", mid); + mid = ((size_t)lo + hi) / 2; litem = PySequence_GetItem(list, mid); if (litem == NULL) return -1; @@ -122,7 +128,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; |