[Python-bugs-list] Integer division can crash under Windows (PR#88)

tim_one@email.msn.com tim_one@email.msn.com
Sun, 26 Sep 1999 12:29:58 -0400 (EDT)


Full_Name: Tim Peters
Version: 1.5.2
OS: Win95
Submission from: 1cust9.tnt4.bos1.da.uu.net (63.21.45.9)


i_divmod doesn't catch overflow when dividing
-sys.maxint-1 by -1.  Under Win95, this has two symptoms:

1. Under the debug build, -sys.maxint-1 is returned.

2. Under the release(!) build, Windows kills the
   interpreter with a system division exception.

The difference is due to different division sequences generated by
the compiler (#2 is triggered by the hardware).

The patch below raises OverflowError in this case.

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

*** Objects/Old/intobject.c	Fri Mar 19 14:30:38 1999
--- Objects/New/intobject.c	Sun Sep 26 11:55:10 1999
***************
*** 434,441 ****
  		return -1;
  	}
  	if (yi < 0) {
! 		if (xi < 0)
  			xdivy = -xi / -yi;
  		else
  			xdivy = - (xi / -yi);
  	}
--- 434,447 ----
  		return -1;
  	}
  	if (yi < 0) {
! 		if (xi < 0) {
! 			if (yi == -1 && -xi < 0) {
! 				/* most negative / -1 */
! 				err_ovf("integer division");
! 				return -1;
! 			}
  			xdivy = -xi / -yi;
+ 		}
  		else
  			xdivy = - (xi / -yi);
  	}