]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merge rev 51711 from the 2.5 branch.
authorTim Peters <tim.peters@gmail.com>
Tue, 5 Sep 2006 02:00:47 +0000 (02:00 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 5 Sep 2006 02:00:47 +0000 (02:00 +0000)
i_divmod():  As discussed on Python-Dev, changed the overflow
checking to live happily with recent gcc optimizations that
assume signed integer arithmetic never overflows.

Misc/NEWS
Objects/intobject.c

index dd8cdf610eab5c5555b1f416e137055c463524e2..9427ab232beb4cd8059171c6da1175baad26d476 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4.4c1?
 Core and builtins
 -----------------
 
+- Overflow checking code in integer division ran afoul of new gcc
+  optimizations.  Changed to be more standard-conforming.
+
 - Patch #1541585: fix buffer overrun when performing repr() on
   a unicode string in a build with wide unicode (UCS-4) support.
 
index 763ed53d4b9cd308df5e5389af530da9c0d05ebc..1a7e2f7c8610a9af5a8acf3ebb2482364736d42e 100644 (file)
@@ -478,8 +478,14 @@ i_divmod(register long x, register long y,
                                "integer division or modulo by zero");
                return DIVMOD_ERROR;
        }
-       /* (-sys.maxint-1)/-1 is the only overflow case. */
-       if (y == -1 && x < 0 && x == -x)
+       /* (-sys.maxint-1)/-1 is the only overflow case.  x is the most
+        * negative long iff x < 0 and, on a 2's-complement box, x == -x.
+        * However, -x is undefined (by C) if x /is/ the most negative long
+        * (it's a signed overflow case), and some compilers care.  So we cast
+        * x to unsigned long first.  However, then other compilers warn about
+        * applying unary minus to an unsigned operand.  Hence the weird "0-".
+        */
+       if (y == -1 && x < 0 && (unsigned long)x == 0-(unsigned long)x)
                return DIVMOD_OVERFLOW;
        xdivy = x / y;
        xmody = x - xdivy * y;