Commit cbdd350d authored by Tim Peters's avatar Tim Peters

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.
parent 3467352f
...@@ -12,6 +12,9 @@ What's New in Python 2.5? ...@@ -12,6 +12,9 @@ What's New in Python 2.5?
Core and builtins 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 - Patch #1541585: fix buffer overrun when performing repr() on
a unicode string in a build with wide unicode (UCS-4) support. a unicode string in a build with wide unicode (UCS-4) support.
...@@ -127,7 +130,7 @@ Library ...@@ -127,7 +130,7 @@ Library
- The __repr__ method of a NULL ctypes.py_object() no longer raises - The __repr__ method of a NULL ctypes.py_object() no longer raises
an exception. an exception.
- uuid.UUID now has a bytes_le attribute. This returns the UUID in - uuid.UUID now has a bytes_le attribute. This returns the UUID in
little-endian byte order for Windows. In addition, uuid.py gained some little-endian byte order for Windows. In addition, uuid.py gained some
workarounds for clocks with low resolution, to stop the code yielding workarounds for clocks with low resolution, to stop the code yielding
duplicate UUIDs. duplicate UUIDs.
...@@ -286,7 +289,7 @@ Library ...@@ -286,7 +289,7 @@ Library
- Bug #1002398: The documentation for os.path.sameopenfile now correctly - Bug #1002398: The documentation for os.path.sameopenfile now correctly
refers to file descriptors, not file objects. refers to file descriptors, not file objects.
- The renaming of the xml package to xmlcore, and the import hackery done - The renaming of the xml package to xmlcore, and the import hackery done
to make it appear at both names, has been removed. Bug #1511497, to make it appear at both names, has been removed. Bug #1511497,
#1513611, and probably others. #1513611, and probably others.
......
...@@ -564,8 +564,14 @@ i_divmod(register long x, register long y, ...@@ -564,8 +564,14 @@ i_divmod(register long x, register long y,
"integer division or modulo by zero"); "integer division or modulo by zero");
return DIVMOD_ERROR; return DIVMOD_ERROR;
} }
/* (-sys.maxint-1)/-1 is the only overflow case. */ /* (-sys.maxint-1)/-1 is the only overflow case. x is the most
if (y == -1 && x < 0 && x == -x) * 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; return DIVMOD_OVERFLOW;
xdivy = x / y; xdivy = x / y;
xmody = x - xdivy * y; xmody = x - xdivy * y;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment