public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Paul Thomas <pault@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-2021] Fortran: Seg fault passing string to type cptr dummy [PR108961].
Date: Wed, 21 Jun 2023 16:02:07 +0000 (GMT)	[thread overview]
Message-ID: <20230621160207.E6CAD3858D28@sourceware.org> (raw)

https://gcc.gnu.org/g:caf0892eea67349d9a1e44590c3440768136fe2b

commit r14-2021-gcaf0892eea67349d9a1e44590c3440768136fe2b
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Wed Jun 21 17:01:57 2023 +0100

    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.

Diff:
---
 gcc/fortran/trans-expr.cc              |  5 ++++-
 gcc/testsuite/gfortran.dg/pr108961.f90 | 26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 45a984b6bdb..3c209bcde97 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -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
index 00000000000..3e6c9df48bb
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr108961.f90
@@ -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

                 reply	other threads:[~2023-06-21 16:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230621160207.E6CAD3858D28@sourceware.org \
    --to=pault@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).