Bug#696058: gfortran: [gfortran] incorrect logic in -O2 old-style
Kevin Mitchell wrote:
Comparision logic fails between "wrapped around" integers inside an inverted
while loop hack. This occurs only on optimisation level 2 and if the
loop does not have an upper limit.
I encountered this when trying to emperically check the integer
limits. Attached is a small example code and the unexpected output when
compiled with "gfortran -O2 ltbug.f". The last line should read
"( 127 < -128 )= F" and execution should terminate there, but instead
I believe that that's a bug in the program. Fortran (like C) does not
define the semantic for integer wrapping around [at least not for signed
integers; though Fortran doesn't have unsigned integers].
Work around: -fno-strict-overflow. (-fstrict-overflow is enabled by
default with -O2 or higher).
I have some trouble pin-pointing it in the Fortran standard; nonetheless
some quotes from Fortran 2008:
* The standard doesn't define the semantics, hence, the catch it all
applies: "A program (2.2.2) is a standard-conforming program if it uses
only those forms and relationships described herein and if the program
has an interpretation according to this part of ISO/IEC 1539." (Section
* "The execution of any numeric operation whose result is not defined by
the arithmetic used by the processor is prohibited." (188.8.131.52.4
Evaluation of numeric intrinsic operations)
* And the numerical model is stated in "13.4 Numeric models".
The following quote is more explicit albeit not normative. It comes from
the draft Fortran appendix to ISO/IEC Technical Report (TR) 24772
"Guidance to avoiding vulnerabilities in programming languages through
language selection and use". [That's one of the few free-of-charge ISO
standards.] The Fortran appendix has been written by members of the
Fortran standardization committee and has been voted on. See
Namely: "if an integer operation results in a value outside the
supported range, the program is not conforming. This might not be
detected. Likewise, assigning a value to an integer variable whose range
does not include the value, renders the program not conforming."