diff -r 7e8ac22a4cf2 Lib/test/test_bisect.py --- a/Lib/test/test_bisect.py Sat Mar 10 09:27:30 2012 +0100 +++ b/Lib/test/test_bisect.py Sat Mar 10 15:27:35 2012 +0000 @@ -122,6 +122,13 @@ self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3), self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3), + def test_large_range(self): + # Issue 13496 + mod = self.module + data = range(sys.maxsize-1) + self.assertEqual(mod.bisect_left(data, sys.maxsize-3), sys.maxsize-3) + self.assertEqual(mod.bisect_right(data, sys.maxsize-3), sys.maxsize-2) + def test_random(self, n=25): from random import randrange for i in range(n): diff -r 7e8ac22a4cf2 Modules/_bisectmodule.c --- a/Modules/_bisectmodule.c Sat Mar 10 09:27:30 2012 +0100 +++ b/Modules/_bisectmodule.c Sat Mar 10 15:27:35 2012 +0000 @@ -21,7 +21,10 @@ 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; @@ -123,7 +126,10 @@ 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;