https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97592

            Bug ID: 97592
           Summary: Incorrectly set pointer remapping with array pointer
                    argument to CONTIGUOUS dummy
           Product: gcc
           Version: og10 (devel/omp/gcc-10)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: reubendb at gmail dot com
  Target Milestone: ---

Hi, 

The following code produces an incorrect results with GCC 10.1.0, 10.2.0.

The code works as expected with GCC/6.4.0,7.4.0,8.1.0,8.1.1 

program RemappingTest
  implicit none
  real, dimension ( :, : ), pointer :: & 
    B_2D
  real, dimension ( :, :, : ), pointer :: &
    B_3D

  !-- Prepare B_2D
  allocate ( B_2D ( 16, 3 ) )
  B_2D  =  - huge ( 1.0 )

  !-- Point B_3D to Storage
  call SetPointer ( B_2D, 4, 4, 3, B_3D )

  !-- Set B_3D
  B_3D  =  2.0

  !-- See if the result is reflected in Storage
  print*
  print*, "B_2D = ", B_2D  !-- expect 2.0 for all elements

contains

  subroutine SetPointer ( C_2D, n1, n2, n3, C_3D )
    real, dimension ( :, : ), intent ( inout ), target, contiguous :: &
      C_2D
    integer :: &
      n1, n2, n3
    real, dimension ( :, :, : ), intent ( inout ), pointer :: &
      C_3D
    C_3D ( 1 : n1, 1 : n2, 1 : n3 )  =>  C_2D
  end subroutine SetPointer

end program RemappingTest

]$ gfortran --version
GNU Fortran (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

]$ gfortran -Wall -Wextra -fno-strict-aliasing -fwrapv -Warray-temporaries
PointerRemapTest.f90 
PointerRemapTest.f90:13:20:

   13 |   call SetPointer ( B_2D, 4, 4, 3, B_3D )
      |                    1
Warning: Creating array temporary at (1) [-Warray-temporaries]

]$ ./a.out 

 B_2D =   -3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38 
-3.40282347E+38  -3.40282347E+38  -3.40282347E+38  -3.40282347E+38

It seems to be issue is because inside the subroutine SetPointer, a new array
`C_2D` is created. So `C_3D` points to this new array instead. Outside of
subroutine, this pointer (now `B_3D`) actually points to an invalid memory
location. In some cases I can trigger a segfault with the code with GCC-10.2.0

Reply via email to