http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58270
Dominique d'Humieres <dominiq at lps dot ens.fr> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |WAITING
Last reconfirmed| |2013-09-02
Ever confirmed|0 |1
--- Comment #15 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
It is invalid to use
subroutine buggy(i1, i2, i3)
integer*4 i1, i2, i3
real*8 dmem
common dmem(1)
dmem(i1)=1.
dmem(i2)=2.
dmem(i3)=2.
return
end
with any i* different from 1. If you compile the code with -fbounds-check (or
for recent gfortran, -fcheck=bounds) you get for 'echo 1 2 3'
Fortran runtime error: Index '2' of dimension 1 of array 'dmem' above upper
bound of 1
As the code is invalid if one of the i* is not one, the compiler can do
whatever it finds appropriate, e.g., set i1=i2=i3=1 (only valid case) and
discard the other assignments.
AFAICT, the following works as I expect (4.0, 2.0, 3.0):
[macbook] dominiq/Downloads% cat buggy.f90
subroutine buggy(i1, i2, i3)
integer*4 i1, i2, i3
real*8 dmem
common dmem(1)
dmem(i1)=4.
! dmem(i2)=2.
! dmem(i3)=2.
return
end
[macbook] dominiq/Downloads% cat main.f90
! Compile and link this file with buggy.f, using gfortran 4.6 (and probably
! any newer version), with optimization enabled (at least -O1).
! Run with: echo 1 2 3 | ./a.out
! expected (correct) result: 1. 2. 2.
program main
implicit none
integer*4 i1,i2,i3
real*8 dmem
common dmem(3)
read (*,*) i1,i2,i3
dmem(i1) = 1.0
dmem(i2) = 2.0
dmem(i3) = 3.0
print *, dmem
call buggy(i1,i2,i3)
write (*,*) dmem(1),dmem(2),dmem(3)
end
I let you close the PR as INVALID.