public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: fix for CHARACTER(len=*) dummies with bind(C) [PR115390]
@ 2024-06-18 20:10 Harald Anlauf
  2024-06-19  7:21 ` Andre Vehreschild
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Anlauf @ 2024-06-18 20:10 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

the attached simple patch fixes warnings for use of uninitialized
temporaries for the string length before being defined.  The cause
is obvious: type sizes were being calculated before the temporaries
were set from the descriptor for the dummy passed to the BIND(C)
procedure.  Wrong code might have been possible as well.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr115390.diff --]
[-- Type: text/x-patch, Size: 2984 bytes --]

From 95a3cefd5e84cf0d393c2606757894389c08ebba Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 18 Jun 2024 21:57:19 +0200
Subject: [PATCH] Fortran: fix for CHARACTER(len=*) dummies with bind(C)
 [PR115390]

gcc/fortran/ChangeLog:

	PR fortran/115390
	* trans-decl.cc (gfc_conv_cfi_to_gfc): Move derivation of type sizes
	for character via gfc_trans_vla_type_sizes to after character length
	has been set.

gcc/testsuite/ChangeLog:

	PR fortran/115390
	* gfortran.dg/bind_c_char_11.f90: New test.
---
 gcc/fortran/trans-decl.cc                    |  4 +-
 gcc/testsuite/gfortran.dg/bind_c_char_11.f90 | 45 ++++++++++++++++++++
 2 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/bind_c_char_11.f90

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index dca7779528b..704f24be84a 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -7063,8 +7063,8 @@ gfc_conv_cfi_to_gfc (stmtblock_t *init, stmtblock_t *finally,
   if (sym->ts.type == BT_CHARACTER
       && !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
     {
-      gfc_conv_string_length (sym->ts.u.cl, NULL, init);
-      gfc_trans_vla_type_sizes (sym, init);
+      gfc_conv_string_length (sym->ts.u.cl, NULL, &block);
+      gfc_trans_vla_type_sizes (sym, &block);
     }

   /* gfc->data = cfi->base_addr - or for scalars: gfc = cfi->base_addr.
diff --git a/gcc/testsuite/gfortran.dg/bind_c_char_11.f90 b/gcc/testsuite/gfortran.dg/bind_c_char_11.f90
new file mode 100644
index 00000000000..5ed8e82853b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bind_c_char_11.f90
@@ -0,0 +1,45 @@
+! { dg-do compile }
+! { dg-additional-options "-Wuninitialized" }
+!
+! PR fortran/115390 - fixes for CHARACTER(len=*) dummies with bind(C)
+
+module test
+  implicit none
+contains
+  subroutine bar(s,t) bind(c)
+    character(*), intent(in) :: s,t
+    optional                 :: t
+    call foo(s,t)
+  end
+  subroutine bar1(s,t) bind(c)
+    character(*), intent(in) :: s(:),t(:)
+    optional                 :: t
+    call foo1(s,t)
+  end
+  subroutine bar4(s,t) bind(c)
+    character(len=*,kind=4), intent(in) :: s,t
+    optional                            :: t
+    call foo4(s,t)
+  end
+  subroutine bar5(s,t) bind(c)
+    character(len=*,kind=4), intent(in) :: s(:),t(:)
+    optional                            :: t
+    call foo5(s,t)
+  end
+  subroutine foo(s,t)
+    character(*), intent(in) :: s,t
+    optional                 :: t
+  end
+  subroutine foo1(s,t)
+    character(*), intent(in) :: s(:),t(:)
+    optional                 :: t
+  end
+  subroutine foo4(s,t)
+    character(len=*,kind=4), intent(in) :: s,t
+    optional                            :: t
+  end
+  subroutine foo5(s,t)
+    character(len=*,kind=4), intent(in) :: s(:),t(:)
+    optional                            :: t
+  end
+end
--
2.35.3


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

* Re: [PATCH] Fortran: fix for CHARACTER(len=*) dummies with bind(C) [PR115390]
  2024-06-18 20:10 [PATCH] Fortran: fix for CHARACTER(len=*) dummies with bind(C) [PR115390] Harald Anlauf
@ 2024-06-19  7:21 ` Andre Vehreschild
  0 siblings, 0 replies; 2+ messages in thread
From: Andre Vehreschild @ 2024-06-19  7:21 UTC (permalink / raw)
  To: Harald Anlauf; +Cc: fortran, gcc-patches

Hi Harald,

that patch looks quite obvious to me. Therefore ok for mainline by me.

Thanks for the patch.

Regards,
	Andre

On Tue, 18 Jun 2024 22:10:55 +0200
Harald Anlauf <anlauf@gmx.de> wrote:

> Dear all,
>
> the attached simple patch fixes warnings for use of uninitialized
> temporaries for the string length before being defined.  The cause
> is obvious: type sizes were being calculated before the temporaries
> were set from the descriptor for the dummy passed to the BIND(C)
> procedure.  Wrong code might have been possible as well.
>
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> Thanks,
> Harald
>


--
Andre Vehreschild * Email: vehre ad gmx dot de

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

end of thread, other threads:[~2024-06-19  7:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-18 20:10 [PATCH] Fortran: fix for CHARACTER(len=*) dummies with bind(C) [PR115390] Harald Anlauf
2024-06-19  7:21 ` Andre Vehreschild

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