]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: reject SAVE of a COMMON in a BLOCK construct [PR119199]
authorHarald Anlauf <anlauf@gmx.de>
Mon, 10 Mar 2025 21:24:27 +0000 (22:24 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Tue, 11 Mar 2025 17:07:52 +0000 (18:07 +0100)
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
gcc/fortran/trans-common.cc
gcc/testsuite/gfortran.dg/common_30.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/common_31.f90 [new file with mode: 0644]

index 5a46658651aa9a617fc11993b905191f16e9b773..feb454ea5b362c4015ebf4a7daeb35df9dc7b43b 100644 (file)
@@ -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;
 
index 70b45174f84a1ada70c6514515a15988f8f60c09..2db50da20dd088fe337f003e742bfaa948b0659c 100644 (file)
@@ -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 (file)
index 0000000..77a8634
--- /dev/null
@@ -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 (file)
index 0000000..b60f46d
--- /dev/null
@@ -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