public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER
@ 2023-06-20 16:19 Paul Richard Thomas
  2023-06-20 16:30 ` Tobias Burnus
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Richard Thomas @ 2023-06-20 16:19 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 321 bytes --]

Dear All,

This patch is verging on obvious. The PR was originally, incorrectly
blocking PR87477 and the testcase has remained in my 'associate'
directory. I thought that it is time to get shot of it!

Is there a better way to detect a type(c_ptr) formal argument?

Subject to advice on the question, OK for trunk?

Paul

[-- Attachment #2: submit.diff --]
[-- Type: text/x-patch, Size: 541 bytes --]

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 45a984b6bdb..0823efd5abc 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -7353,6 +7353,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 	 need the length.  */
       if (parmse.string_length != NULL_TREE
 	  && !sym->attr.is_bind_c
+	  && !(fsym && fsym->ts.type == BT_DERIVED
+	       && !strcmp (fsym->ts.u.derived->name, "c_ptr"))
 	  && !(fsym && UNLIMITED_POLY (fsym)))
 	vec_safe_push (stringargs, parmse.string_length);
 

[-- Attachment #3: Change.Logs --]
[-- Type: application/octet-stream, Size: 337 bytes --]

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

2023-06-20  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.

[-- Attachment #4: pr108961.f90 --]
[-- Type: text/x-fortran, Size: 901 bytes --]

! { 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER
  2023-06-20 16:19 [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER Paul Richard Thomas
@ 2023-06-20 16:30 ` Tobias Burnus
  2023-06-20 20:47   ` Mikael Morin
  0 siblings, 1 reply; 4+ messages in thread
From: Tobias Burnus @ 2023-06-20 16:30 UTC (permalink / raw)
  To: Paul Richard Thomas; +Cc: gcc-patches, fortran

On 20.06.23 18:19, Paul Richard Thomas via Fortran wrote:

> Is there a better way to detect a type(c_ptr) formal argument?
u.derived->intmod_sym_id == ISOCBINDING_PTR ?

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER
  2023-06-20 16:30 ` Tobias Burnus
@ 2023-06-20 20:47   ` Mikael Morin
  2023-06-21 16:08     ` Paul Richard Thomas
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Morin @ 2023-06-20 20:47 UTC (permalink / raw)
  To: Tobias Burnus, Paul Richard Thomas; +Cc: gcc-patches, fortran

Le 20/06/2023 à 18:30, Tobias Burnus a écrit :
> On 20.06.23 18:19, Paul Richard Thomas via Fortran wrote:
> 
>> Is there a better way to detect a type(c_ptr) formal argument?
> u.derived->intmod_sym_id == ISOCBINDING_PTR ?
	&& u.derived->from_intmod == INTMOD_ISO_C_BINDING ?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER
  2023-06-20 20:47   ` Mikael Morin
@ 2023-06-21 16:08     ` Paul Richard Thomas
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Richard Thomas @ 2023-06-21 16:08 UTC (permalink / raw)
  To: Mikael Morin; +Cc: Tobias Burnus, gcc-patches, fortran

Committed as r14-2021-gcaf0892eea67349d9a1e44590c3440768136fe2b

Thanks for the pointers, Tobias and Mikael, I used them both.

Paul

On Tue, 20 Jun 2023 at 21:47, Mikael Morin <morin-mikael@orange.fr> wrote:
>
> Le 20/06/2023 à 18:30, Tobias Burnus a écrit :
> > On 20.06.23 18:19, Paul Richard Thomas via Fortran wrote:
> >
> >> Is there a better way to detect a type(c_ptr) formal argument?
> > u.derived->intmod_sym_id == ISOCBINDING_PTR ?
>         && u.derived->from_intmod == INTMOD_ISO_C_BINDING ?
>


-- 
"If you can't explain it simply, you don't understand it well enough"
- Albert Einstein

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-21 16:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-20 16:19 [Patch, fortran] PR108961 - Segfault when associating to pointer from C_F_POINTER Paul Richard Thomas
2023-06-20 16:30 ` Tobias Burnus
2023-06-20 20:47   ` Mikael Morin
2023-06-21 16:08     ` Paul Richard Thomas

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).