diff Modules/_bisectmodule.c @ 76325:709af2d0e862 2.7

Issue 13496: Fix bisect.bisect overflow bug for large collections.
author Mark Dickinson <mdickinson@enthought.com>
date Sun, 15 Apr 2012 16:43:19 +0100
parents b36cb4602e21
children bfe2d1acc09d
line wrap: on
line diff
--- a/Modules/_bisectmodule.c	Sun Apr 15 15:19:06 2012 +0100
+++ b/Modules/_bisectmodule.c	Sun Apr 15 16:43:19 2012 +0100
@@ -21,7 +21,13 @@
             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 @@
             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;