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

            Bug ID: 69695
           Summary: slice of an array retains pointer attribute
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fastjar
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Joost.VandeVondele at mat dot ethz.ch
  Target Milestone: ---

The following testcase:

> cat test.f90
module point
  implicit none
  type point_type
    integer, dimension(:,:), pointer :: array
  end type point_type
contains
  subroutine ptest(a)
    integer, dimension(:), intent(in), pointer :: a
    write(*,*) a**2
  end subroutine ptest
end module point
program test
  use point
  implicit none
  integer :: i, j
  type(point_type), pointer        :: p1
  integer, dimension(:,:), pointer :: a
  allocate(p1)
  allocate(p1%array(5,2))
  p1%array=42
  a => p1%array
  call ptest(a(:,2))
end program test

returns a valgrind error and seemingly wrong output:

> gfortran -g test.f90 
> valgrind ./a.out
==81284== Memcheck, a memory error detector
==81284== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==81284== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==81284== Command: ./a.out
==81284== 
==81284== Invalid read of size 4
==81284==    at 0x400920: __point_MOD_ptest (test.f90:9)
==81284==    by 0x400B64: MAIN__ (test.f90:22)
==81284==    by 0x400BA3: main (test.f90:13)
==81284==  Address 0x4dbf458 is 0 bytes after a block of size 40 alloc'd
==81284==    at 0x4A06B3F: malloc (vg_replace_malloc.c:299)
==81284==    by 0x4009B9: MAIN__ (test.f90:19)
==81284==    by 0x400BA3: main (test.f90:13)
==81284== 
        1764        1764        1764        1764           0
==81284== 
==81284== HEAP SUMMARY:
==81284==     in use at exit: 112 bytes in 2 blocks
==81284==   total heap usage: 21 allocs, 19 frees, 11,952 bytes allocated
==81284== 
==81284== LEAK SUMMARY:
==81284==    definitely lost: 72 bytes in 1 blocks
==81284==    indirectly lost: 40 bytes in 1 blocks
==81284==      possibly lost: 0 bytes in 0 blocks
==81284==    still reachable: 0 bytes in 0 blocks
==81284==         suppressed: 0 bytes in 0 blocks
==81284== Rerun with --leak-check=full to see details of leaked memory
==81284== 
==81284== For counts of detected and suppressed errors, rerun with: -v
==81284== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 5 from 5)

However, the underlying problem is that gfortran doesn't generate a compile
time error, as the array slice is passed to a subroutine that expects a pointer
argument. Ifort diagnoses this clearly:

> ifort test.f90
test.f90(22): error #7121: A ptr dummy may only be argument associated with a
ptr, and this array element or section does not inherit the POINTER attr from
its parent array.   [A]
  call ptest(a(:,2))
-------------^
compilation aborted for test.f90 (code 1)

I think this should be diagnosed by gfortran as well.

Reply via email to