Bug 32153 - runtime integer overflow checking fails
Summary: runtime integer overflow checking fails
Status: RESOLVED DUPLICATE of bug 19020
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-30 10:10 UTC by Jos de Kloe
Modified: 2023-10-27 17:53 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jos de Kloe 2007-05-30 10:10:37 UTC
When compiling my code with the -ftrapv option, I would have expected the program to detect integer overflows. However, this only seems to happen for the 8-byte integer type, and not for the other integer types.

Maybe I have a wrong expectation here, and it should only check 8-byte integers? If this is the case, I would like to request implementation of an additional option to do this runtime check for the other types as well (which may be very usefull for debugging purposes).

Here are the code fragments that I used for testing and the results:

>gfortran -v
Using built-in specs.
Target: i386-pc-linux-gnu
Configured with: /home/fxcoudert/gfortran_nightbuild/trunk/configure --prefix=/home/fxcoudert/gfortran_nightbuild/irun-20070529 --enable-languages=c,fortran --build=i386-pc-linux-gnu --enable-checking=release --with-gmp=/home/fxcoudert/gfortran_nightbuild/software
Thread model: posix
gcc version 4.3.0 20070529 (experimental)

>cat TestInt1.F90
program Test1

  integer(1) :: i,j

  i=126 ! max for this type is 127
  do j=1,10
     print *,"i=",i
     i = i + 1
  enddo

end program Test1

>gfortran -ftrapv -o TestInt1 TestInt1.F90

>TestInt1
 i=  126
 i=  127
 i= -128
 i= -127
 i= -126
 i= -125
 i= -124
 i= -123
 i= -122
 i= -121

>cat TestInt2.F90
program Test2

  integer(2) :: i,j

  i=32767-1 ! max for this type is 32767
  do j=1,10
     print *,"i=",i
     i = i + 1
  enddo

end program Test2

>gfortran -ftrapv -o TestInt2 TestInt2.F90

>TestInt2
 i=  32766
 i=  32767
 i= -32768
 i= -32767
 i= -32766
 i= -32765
 i= -32764
 i= -32763
 i= -32762
 i= -32761

>cat TestInt4.F90
program Test4

  integer(4) :: i,j

  i=2147483647-1 ! max for this type is 2147483647
  do j=1,10
     print *,"i=",i
     i = i + 1
  enddo

end program Test4

>gfortran -ftrapv -o TestInt4 TestInt4.F90

>TestInt4
 i=  2147483646
 i=  2147483647
 i= -2147483648
 i= -2147483647
 i= -2147483646
 i= -2147483645
 i= -2147483644
 i= -2147483643
 i= -2147483642
 i= -2147483641

>cat TestInt8.F90
program Test8

  integer(8) :: i,j

  i=9223372036854775807_8-1 ! max for this type is 9223372036854775807
  do j=1,10
     print *,"i=",i
     i = i + 1
  enddo

end program Test8

>gfortran -ftrapv -o TestInt8 TestInt8.F90

>TestInt8
 i=  9223372036854775806
 i=  9223372036854775807
Abort

best regards,

Jos de Kloe
Comment 1 Tobias Burnus 2007-05-30 17:54:19 UTC
-ftrapv only traps floating-point overflow - no integer overflows.

(This is not completely correct, as some systems seem to offer to send FPE also for integer overflows, see e.g.
https://www.cisl.ucar.edu/docs/ibm/ref/fpe.html
For x86 (32/64) platforms, this does not seem to be directly supported by hardware; in this case one would need to add manual checks, which are slow, cf. e.g. http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/30227217.aspx .)

-> WONTFIX, unless you find a good method to trap integer overflows.
Comment 2 Eric Botcazou 2007-05-30 18:02:38 UTC
> -ftrapv only traps floating-point overflow - no integer overflows.

It's the other way around, -ftrapv is meant to trap on integer operations only.

But it's well-known for being broken...
Comment 3 Richard Biener 2007-05-30 19:59:13 UTC
Reopen...
Comment 4 Richard Biener 2007-05-30 19:59:34 UTC
...to mark as dup of PR19020.

*** This bug has been marked as a duplicate of 19020 ***
Comment 5 Jos de Kloe 2007-06-01 08:41:02 UTC
(In reply to comment #1)
> for integer overflows, see e.g.
> https://www.cisl.ucar.edu/docs/ibm/ref/fpe.html
> For x86 (32/64) platforms, this does not seem to be directly supported by
> hardware; in this case one would need to add manual checks, which are slow, cf.
...
> -> WONTFIX, unless you find a good method to trap integer overflows.

thanks for your replies.
I understand adding extra checks makes the compiled code slower, if this cannot be done by the hardware (but the same is probably true for many other runtime checks, like bounds checking, etc.)
However, I think it would be very usefull to have such an option for debugging purposes. If anyone finds the time to implement it, it would be highly appreciated.

best regards,

Jos de Kloe