]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Seg fault passing string to type cptr dummy [PR108961].
authorPaul Thomas <pault@gcc.gnu.org>
Wed, 21 Jun 2023 16:01:57 +0000 (17:01 +0100)
committerPaul Thomas <pault@gcc.gnu.org>
Wed, 21 Jun 2023 16:01:57 +0000 (17:01 +0100)
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.

gcc/fortran/trans-expr.cc
gcc/testsuite/gfortran.dg/pr108961.f90 [new file with mode: 0644]

index 45a984b6bdb164a077518f7029aabbb03d19c1e3..3c209bcde97c95b5ee9180d0d82021100a9f956b 100644 (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);
 
diff --git a/gcc/testsuite/gfortran.dg/pr108961.f90 b/gcc/testsuite/gfortran.dg/pr108961.f90
new file mode 100644 (file)
index 0000000..3e6c9df
--- /dev/null
@@ -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