[patch] Integer overflow when folding constants

Michael Meissner meissner@cygnus.com
Wed Apr 4 16:37:00 GMT 2001


On Wed, Apr 04, 2001 at 04:20:47PM -0400, Diego Novillo wrote:
> The following program crashes the compiler with a SIGFPE when
> using -O2:
> 
> -----------------------------------------------------------------------------
> #include <limits.h>
> #include <stdio.h>
> 
> main()
> {
>   int a = INT_MIN;
>   int b = -1;
>   printf ("%d\n", a / b);
> }
> -----------------------------------------------------------------------------
> 
> This happens because simplify_binary_operation tries to fold
> INT_MIN/-1 without checking for overflow. 
> 
> This patch fixes the problem and adds a new test case in
> c-torture/compile. Bootstrapped on branch on i686-pc-linux-gnu.

Just as a note, it depends on your host platform whether you get a trap or
not.  I also suspect your patch may or may not do the right thing on a 64-bit
host compiling for a 32-bit target.

-- 
Michael Meissner, Red Hat, Inc.  (GCC group)
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work:	  meissner@redhat.com		phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org	fax:   +1 978-692-4482



More information about the Gcc-patches mailing list