Fortran: fix ICE with bind(c) in block data [PR104332]

Message ID trinity-c56ca7fa-4444-483d-9c3e-93f641dd7f22-1678385289835@3c-app-gmx-bap32
State Accepted
Headers
Series Fortran: fix ICE with bind(c) in block data [PR104332] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Harald Anlauf March 9, 2023, 6:08 p.m. UTC
  Dear all,

the attached almost obvious patch fixes a NULL pointer dereference
in a check of a symbol with the bind(c) attribute.

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

This PR is marked as 10/11/12/13 regression, thus it should
qualify for a backport.  It's simple enough anyway.

Thanks,
Harald
  

Comments

Jerry D March 10, 2023, 3:59 a.m. UTC | #1
On 3/9/23 10:08 AM, Harald Anlauf via Fortran wrote:
> Dear all,
> 
> the attached almost obvious patch fixes a NULL pointer dereference
> in a check of a symbol with the bind(c) attribute.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
> This PR is marked as 10/11/12/13 regression, thus it should
> qualify for a backport.  It's simple enough anyway.
> 
> Thanks,
> Harald
> 

OK, please proceed. Thanks for the patch.

Jerry
  

Patch

From ef96d7d360c088d68e3b405401bdb8b589d562f2 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 9 Mar 2023 18:59:08 +0100
Subject: [PATCH] Fortran: fix ICE with bind(c) in block data [PR104332]

gcc/fortran/ChangeLog:

	PR fortran/104332
	* resolve.cc (resolve_symbol): Avoid NULL pointer dereference while
	checking a symbol with the BIND(C) attribute.

gcc/testsuite/ChangeLog:

	PR fortran/104332
	* gfortran.dg/bind_c_usage_34.f90: New test.
---
 gcc/fortran/resolve.cc                        |  4 ++--
 gcc/testsuite/gfortran.dg/bind_c_usage_34.f90 | 21 +++++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/bind_c_usage_34.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 2780c82c798..46585879ddc 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -15933,8 +15933,8 @@  resolve_symbol (gfc_symbol *sym)

       /* First, make sure the variable is declared at the
 	 module-level scope (J3/04-007, Section 15.3).	*/
-      if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
-          sym->attr.in_common == 0)
+      if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
+	  && !sym->attr.in_common)
 	{
 	  gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
 		     "is neither a COMMON block nor declared at the "
diff --git a/gcc/testsuite/gfortran.dg/bind_c_usage_34.f90 b/gcc/testsuite/gfortran.dg/bind_c_usage_34.f90
new file mode 100644
index 00000000000..40c8e9363cf
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bind_c_usage_34.f90
@@ -0,0 +1,21 @@ 
+! { dg-do compile }
+! PR fortran/104332 - ICE with bind(c) in block data
+! Contributed by G. Steinmetz
+
+block data
+  bind(c) :: a ! { dg-error "cannot be BIND\\(C\\)" }
+end
+
+block data aa
+   real, bind(c) :: a ! { dg-error "cannot be BIND\\(C\\)" }
+end
+
+block data bb
+   real    :: a ! { dg-error "cannot be BIND\\(C\\)" }
+   bind(c) :: a
+end
+
+block data cc
+   common /a/ x
+   bind(c) :: /a/
+end
--
2.35.3