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

--- Comment #3 from Paul Thomas <pault at gcc dot gnu.org> ---
Created attachment 49793
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49793&action=edit
Fix for the PR

This regtests OK. Testcase:

! { dg-do run }
!
! Test the fix for PR98342.
!
! Contributed by Martin Stein  <ms...@gmx.net>
!
module mod
implicit none
private
public get_tuple, sel_rank

! we need to encapsulate the allocatable in a derived type
type, public :: tuple
   integer, dimension(:), allocatable :: t
! with pointer there is no failure
! integer, dimension(:), pointer :: t
end type tuple

contains

function sel_rank(x) result(s)
   character(len=:), allocatable :: s
   type(tuple), dimension(..), intent(in) :: x
   select rank (x)
   rank (0)
      s = '0'
   rank (1)
      s = '1'
   rank default
      s = '?'
   end select
! this is printed as expected
!   print *, 'rank: ', s
end function sel_rank

function get_tuple(t) result(a)
   type(tuple) :: a
   integer, dimension(:), intent(in) :: t
   allocate(a%t, source=t)
end function get_tuple

end module mod


program alloc_rank
   use mod
   implicit none

   integer, dimension(1:3) :: x
   character(len=:), allocatable :: output
   type(tuple) :: z

   x = [1,2,3]
! this should print '0' (as a tuple itself has rank 0)
   output = sel_rank(get_tuple(x))   ! runtime: Error in `./alloc_rank.x':
                                     ! munmap_chunk(): invalid pointer
                                     ! (memory freed not malloc'ed)
   if (output .ne. '0') stop 1

   output = sel_rank([get_tuple(x)]) ! This worked OK
   if (output .ne. '1') stop 2

   deallocate(output)
end program alloc_rank

Paul

Reply via email to