Fortran: reject SAVE of a COMMON in a BLOCK construct [PR119199]

Message ID 2983cd1c-4d44-4973-9390-57ffb55a5fae@gmx.de
State New
Headers
Series Fortran: reject SAVE of a COMMON in a BLOCK construct [PR119199] |

Commit Message

Harald Anlauf March 10, 2025, 9:31 p.m. UTC
  Dear all,

here's a patch that fixes a 15-regression (NULL pointer dereference)
and implements a missed check of SAVE of a COMMON in a BLOCK construct.

Two separate testcases, as the respective error messages occur at
different stages in the frontend.

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

Thanks,
Harald
  

Comments

Thomas Koenig March 11, 2025, 6:13 a.m. UTC | #1
Hi Harald,

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

Looks good to me.

Thanks for the patch!

Best regards

	Thomas
  
Harald Anlauf March 11, 2025, 7:59 p.m. UTC | #2
Hi Thomas!

Am 11.03.25 um 07:13 schrieb Thomas Koenig:
> Hi Harald,
>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> Looks good to me.
>
> Thanks for the patch!

Committed as r15-7966-g5e9f71254a5276.

Thanks for the review!

> Best regards
>
>      Thomas
>
>
  

Patch

From 7e8f9a0d625f1c66014f71775190218b8ac51e4b Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 10 Mar 2025 22:24:27 +0100
Subject: [PATCH] Fortran: reject SAVE of a COMMON in a BLOCK construct
 [PR119199]

	PR fortran/119199

gcc/fortran/ChangeLog:

	* decl.cc (gfc_match_save): Reject SAVE statement of a COMMON block
	when in a BLOCK construct.
	* trans-common.cc (translate_common): Avoid NULL pointer dereference.

gcc/testsuite/ChangeLog:

	* gfortran.dg/common_30.f90: New test.
	* gfortran.dg/common_31.f90: New test.
---
 gcc/fortran/decl.cc                     |  9 +++++++++
 gcc/fortran/trans-common.cc             |  2 +-
 gcc/testsuite/gfortran.dg/common_30.f90 | 10 ++++++++++
 gcc/testsuite/gfortran.dg/common_31.f90 | 15 +++++++++++++++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/common_30.f90
 create mode 100644 gcc/testsuite/gfortran.dg/common_31.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 5a46658651a..feb454ea5b3 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -9795,6 +9795,15 @@  gfc_match_save (void)
       if (m == MATCH_NO)
 	goto syntax;
 
+      /* F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
+	 saved-entity-list that does not specify a common-block-name.  */
+      if (gfc_current_state () == COMP_BLOCK)
+	{
+	  gfc_error ("SAVE of COMMON block %qs at %C is not allowed "
+		     "in a BLOCK construct", n);
+	  return MATCH_ERROR;
+	}
+
       c = gfc_get_common (n, 0);
       c->saved = 1;
 
diff --git a/gcc/fortran/trans-common.cc b/gcc/fortran/trans-common.cc
index 70b45174f84..2db50da20dd 100644
--- a/gcc/fortran/trans-common.cc
+++ b/gcc/fortran/trans-common.cc
@@ -1218,7 +1218,7 @@  translate_common (gfc_common_head *common, gfc_symbol *var_list)
   align = 1;
   saw_equiv = false;
 
-  if (var_list->attr.omp_allocate)
+  if (var_list && var_list->attr.omp_allocate)
     gfc_error ("Sorry, !$OMP allocate for COMMON block variable %qs at %L "
 	       "not supported", common->name, &common->where);
 
diff --git a/gcc/testsuite/gfortran.dg/common_30.f90 b/gcc/testsuite/gfortran.dg/common_30.f90
new file mode 100644
index 00000000000..77a86348f4a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/common_30.f90
@@ -0,0 +1,10 @@ 
+! { dg-do compile }
+! PR fortran/119199
+!
+! One cannot SAVE an undefined COMMON block
+!
+! Contributed by David Binderman
+
+program main
+  save /argmnt1/ ! { dg-error "does not exist" }
+end
diff --git a/gcc/testsuite/gfortran.dg/common_31.f90 b/gcc/testsuite/gfortran.dg/common_31.f90
new file mode 100644
index 00000000000..b60f46d7d5a
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/common_31.f90
@@ -0,0 +1,15 @@ 
+! { dg-do compile }
+! PR fortran/119199 - reject SAVE of a COMMON in a BLOCK construct
+!
+! F2023:C1108: A SAVE statement in a BLOCK construct shall contain a
+!              saved-entity-list that does not specify a common-block-name.
+!
+! Contributed by David Binderman
+
+program main
+  real r
+  common /argmnt2/ r
+  block
+    save /argmnt2/ ! { dg-error "not allowed in a BLOCK construct" }
+  end block
+end
-- 
2.43.0