[Bug fortran/106317] New: deferred-length character array pointer in derived type

2022-07-15 Thread clange001 at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106317

Bug ID: 106317
   Summary: deferred-length character array pointer in derived
type
   Product: gcc
   Version: 11.3.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: clange001 at gmail dot com
  Target Milestone: ---

The following test program gives seg fault with fortran 11.3.  I'm trying to
store a pointer to a deferred-length character array.  The pointer I'd like to
store is in a derived type.  A regular fortran intrinsic pointer outside of the
derived type works fine, but a pointer stored in the derived type does not.  My
apologies if this has been addressed or reported elsewhere.


$ gfortran --version
GNU Fortran (MacPorts gcc11 11.3.0_1) 11.3.0
Copyright (C) 2021 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.

$ cat test.f90
program test

  type t
character(:), pointer, dimension(:) :: a_ptr
  end type
  type(t) :: mt
  character(:), pointer, dimension(:) :: a
  character(:), pointer, dimension(:) :: b_ptr

  allocate(character(len=10) :: a(3))
  a(:) = '0123456789'
  print *, a

  ! this works fine
  b_ptr => a
  print *, associated(b_ptr, a)
  print *, b_ptr

  ! does not seem work if pointer is member of derived type
  mt%a_ptr => a
  print *, associated(mt%a_ptr, a)
  print *, len(mt%a_ptr)  ! string length is NOT correct
  print *, size(mt%a_ptr) ! array size IS correct
  print *, mt%a_ptr   ! seg fault trying to access

  print *, 'end'

end program test

$ gfortran test.f90
$ ./a.out
 012345678901234567890123456789
 T
 012345678901234567890123456789
 T
   328630909
   3

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

Backtrace for this error:
#0  0x10b2f4590
#1  0x10b2f3c1e
#2  0x7fff205b5d7c
#3  0x10b42d2c5
#4  0x10b430664
#5  0x10b4310bb
#6  0x10b2e0cfd
#7  0x10b2e0dc6
zsh: segmentation fault  ./a.out

[Bug fortran/89352] New: Deferred length character array pointer error with gfortran

2019-02-14 Thread clange001 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89352

Bug ID: 89352
   Summary: Deferred length character array pointer error with
gfortran
   Product: gcc
   Version: 8.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: fortran
  Assignee: unassigned at gcc dot gnu.org
  Reporter: clange001 at gmail dot com
  Target Milestone: ---

I have two deferred length character arrays in gfortran.  I allocate one of
them, and then point the other one to it.  The pointer variable has the correct
array size, but not the correct character length.  The program terminates with
an error when trying to access the information in the pointer.

I see that there may be related bugs that have been fixed
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51976), but this one is present
now in version 8.

I'm testing this with:
GNU Fortran (MacPorts gcc8 8.2.0_2) 8.2.0

and also
GNU Fortran (MacPorts gcc8 8.2.0_3) 8.2.0


The code below works fine with Intel Fortran on Windows.

program test
  implicit none
  character(len=:), pointer, dimension(:) :: charvara
  character(len=:), pointer, dimension(:) :: charvara_ptr

  allocate(character(len=10) :: charvara(3))
  charvara(:) = '0123456789'
  print *, 'len(charvara) = ', len(charvara)
  print *, 'size(charvara) = ', size(charvara)
  print *, 'charvara = ', charvara

  charvara_ptr => charvara
  print *, 'associated = ', associated(charvara_ptr, charvara)
  print *, 'len(charvara_ptr) = ', len(charvara_ptr)
  print *, 'size(charvara_ptr) = ', size(charvara_ptr)
  print *, 'charvara_ptr = ', charvara_ptr

  print *, 'end'

end program test

I would expect the length, size, and contents of charvara_ptr to be the same as
charvara, but instead I get the following:

 len(charvara) =   10
 size(charvara) =3
 charvara = 012345678901234567890123456789
 associated =  T
 len(charvara_ptr) =   1708412736
 size(charvara_ptr) =3
test(71321,0x7fff9e47c380) malloc: *** mach_vm_map(size=140734901800960)
failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Operating system error: Cannot allocate memory
Memory allocation failure in xrealloc

Error termination. Backtrace:
#0  0x10bb11f8f
#1  0x10bb127fd
#2  0x10bb12987
#3  0x10bb119f9
#4  0x10bbc8950
#5  0x10bbbf147
#6  0x10bbc4b61
#7  0x10bbc7cd8
#8  0x10bbc877e
#9  0x10bb0ccab
#10  0x10bb0cd6e