From: Harald Anlauf <anlauf@gmx.de>
To: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Fortran: error handling of global entity appearing in COMMON block [PR103259]
Date: Tue, 7 Feb 2023 20:36:33 +0100 [thread overview]
Message-ID: <trinity-157fad65-8e00-4b72-b203-2166df85c671-1675798593924@3c-app-gmx-bs54> (raw)
[-- Attachment #1: Type: text/plain, Size: 481 bytes --]
Dear all,
the attached trivial patch by Steve fixes a NULL pointer dereference
that occurs when an error shall be emitted for a global entity that
conflicts with a symbol appearing in a COMMON block, but the symbol's
location is not set. This may happen e.g. in the testcase in the PR,
where the COMMON variables are imported from a module.
Regtested on x86_64-pc-linux-gnu with no failures.
I intend to commit as obvious within 24h unless there are comments.
Thanks,
Harald
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr103259.diff --]
[-- Type: text/x-patch, Size: 2278 bytes --]
From 1c225608e0cabbe909cc4d6e69fb0281bb99418f Mon Sep 17 00:00:00 2001
From: Steve Kargl <kargl@gcc.gnu.org>
Date: Tue, 7 Feb 2023 20:18:42 +0100
Subject: [PATCH] Fortran: error handling of global entity appearing in COMMON
block [PR103259]
gcc/fortran/ChangeLog:
PR fortran/103259
* resolve.cc (resolve_common_vars): Avoid NULL pointer dereference
when a symbol's location is not set.
gcc/testsuite/ChangeLog:
PR fortran/103259
* gfortran.dg/pr103259.f90: New test.
---
gcc/fortran/resolve.cc | 12 +++++++++---
gcc/testsuite/gfortran.dg/pr103259.f90 | 11 +++++++++++
2 files changed, 20 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pr103259.f90
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 100b2382e22..549916c2b53 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -953,9 +953,15 @@ resolve_common_vars (gfc_common_head *common_block, bool named_common)
{
gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
- gfc_error_now ("Global entity %qs at %L cannot appear in a "
- "COMMON block at %L", gsym->name,
- &gsym->where, &csym->common_block->where);
+ {
+ if (csym->common_block)
+ gfc_error_now ("Global entity %qs at %L cannot appear in a "
+ "COMMON block at %L", gsym->name,
+ &gsym->where, &csym->common_block->where);
+ else
+ gfc_error_now ("Global entity %qs at %L cannot appear in a "
+ "COMMON block", gsym->name, &gsym->where);
+ }
/* gfc_add_in_common may have been called before, but the reported errors
have been ignored to continue parsing.
diff --git a/gcc/testsuite/gfortran.dg/pr103259.f90 b/gcc/testsuite/gfortran.dg/pr103259.f90
new file mode 100644
index 00000000000..f78ff26180e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr103259.f90
@@ -0,0 +1,11 @@
+! { dg-do compile }
+! PR fortran/103259 - ICE in resolve_common_vars
+! Contributed by G.Steinmetz
+
+module m
+ integer :: p
+ common /c/ p
+end
+program p ! { dg-error "cannot appear in a COMMON block" }
+ use m ! { dg-error "is also the name of the current program unit" }
+end
--
2.35.3
reply other threads:[~2023-02-07 19:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=trinity-157fad65-8e00-4b72-b203-2166df85c671-1675798593924@3c-app-gmx-bs54 \
--to=anlauf@gmx.de \
--cc=fortran@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).