Fortran: Seg fault passing string to type cptr dummy [PR108961].

2023-06-21  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
	PR fortran/108961
	* trans-expr.cc (gfc_conv_procedure_call): The hidden string
	length must not be passed to a formal arg of type(cptr).

gcc/testsuite/
	PR fortran/108961
	* gfortran.dg/pr108961.f90: New test.
This commit is contained in:
Paul Thomas 2023-06-21 17:01:57 +01:00
parent b9401c3a32
commit caf0892eea
2 changed files with 30 additions and 1 deletions

View file

@ -7348,11 +7348,14 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
}
/* Character strings are passed as two parameters, a length and a
pointer - except for Bind(c) which only passes the pointer.
pointer - except for Bind(c) and c_ptrs which only passe the pointer.
An unlimited polymorphic formal argument likewise does not
need the length. */
if (parmse.string_length != NULL_TREE
&& !sym->attr.is_bind_c
&& !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
&& fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
&& fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
&& !(fsym && UNLIMITED_POLY (fsym)))
vec_safe_push (stringargs, parmse.string_length);

View file

@ -0,0 +1,26 @@
! { dg-do run }
!
! Contributed by Jeffrey Hill <jeffrey.p.hill@nasa.gov>
!
module associate_ptr
use iso_c_binding
contains
subroutine c_f_strpointer(cptr, ptr2)
type(c_ptr), target, intent(in) :: cptr
character(kind=c_char,len=4), pointer :: ptr1
character(kind=c_char,len=:), pointer, intent(out) :: ptr2
call c_f_pointer(cptr, ptr1)
if (ptr1 .ne. 'abcd') stop 1
ptr2 => ptr1 ! Failed here
end subroutine
end module
program test_associate_ptr
use associate_ptr
character(kind=c_char, len=1), target :: char_array(7)
character(kind=c_char,len=:), pointer :: ptr2
char_array = ['a', 'b', 'c', 'd', c_null_char, 'e', 'f']
! The first argument was providing a constant hidden string length => segfault
call c_f_strpointer(c_loc(char_array), ptr2)
if (ptr2 .ne. 'abcd') stop 2
end program