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

--- Comment #3 from janus at gcc dot gnu.org ---
The code in comment 0 seems to have a small error: a%ll is allocated with only
one element (1:1), which should probably be ten (1:10). So I wonder why it runs
with gfortran-7 at all?!?

In any case, here is a corrected and slightly reduced version:


program ptr_alloc

   type :: t
      class(*), allocatable :: val
   end type

   type :: list
      type(t), dimension(:), pointer :: ll
   end type

   integer :: i
   type(list) :: a

   allocate(a%ll(1:2))
   do i = 1,2
      print *,i
      allocate(a%ll(i)%val, source=i)
   end do

   call rrr(a)

contains

   subroutine rrr(a)
      type(list), intent(in) :: a

      integer :: s, j
      type(t), dimension(:), allocatable :: mm

      s = ubound(a%ll,1)
      allocate(mm(1:s))

      do j = 1, s
         print *," ",j
         if (allocated(a%ll(j)%val)) then
            allocate(mm(j)%val, source=a%ll(j)%val)
         end if
      end do
   end subroutine

end


This shows basically the same behavior: It runs with gfortran-7 and earlier
(and also with ifort, pgi, flang), but segfaults with gfortran-8, where
valgrind shows:

==24028== Memcheck, a memory error detector
==24028== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24028== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==24028== Command: ./a.out
==24028== 
           1
           2
             1
==24028== Use of uninitialised value of size 8
==24028==    at 0x40170A: rrr.3800 (c3.f90:36)
==24028==    by 0x4010CF: MAIN__ (c3.f90:20)
==24028==    by 0x401EF2: main (c3.f90:20)
==24028== 
==24028== Use of uninitialised value of size 8
==24028==    at 0x4017E9: rrr.3800 (c3.f90:36)
==24028==    by 0x4010CF: MAIN__ (c3.f90:20)
==24028==    by 0x401EF2: main (c3.f90:20)
==24028== 
==24028== Use of uninitialised value of size 8
==24028==    at 0x4018BD: rrr.3800 (c3.f90:36)
==24028==    by 0x4010CF: MAIN__ (c3.f90:20)
==24028==    by 0x401EF2: main (c3.f90:20)
==24028== 
             2
==24028== Invalid read of size 8
==24028==    at 0x4017E9: rrr.3800 (c3.f90:36)
==24028==    by 0x4010CF: MAIN__ (c3.f90:20)
==24028==    by 0x401EF2: main (c3.f90:20)
==24028==  Address 0xb285ac0 is not stack'd, malloc'd or (recently) free'd
==24028== 

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Reply via email to