public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH, committed] Fortran: error recovery after invalid use of CLASS variable  [PR103475]
Date: Mon, 13 Feb 2023 22:13:18 +0100	[thread overview]
Message-ID: <trinity-4f91f3ff-1a0f-441e-ae8b-8beffd701aa9-1676322798018@3c-app-gmx-bap61> (raw)

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

Dear all,

the attached simple and obvious patch fixes a NULL pointer dereference
on an invalid use of a CLASS variable.

Committed to mainline after regtesting on x86_64-pc-linux-gnu as

https://gcc.gnu.org/g:2ce7e2a83e18a27fe9c659f8667fc24f0df4ea9a

Thanks,
Harald


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

From 2ce7e2a83e18a27fe9c659f8667fc24f0df4ea9a Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 13 Feb 2023 22:02:44 +0100
Subject: [PATCH] Fortran: error recovery after invalid use of CLASS variable
 [PR103475]

gcc/fortran/ChangeLog:

	PR fortran/103475
	* primary.cc (gfc_expr_attr): Avoid NULL pointer dereference for
	invalid use of CLASS variable.

gcc/testsuite/ChangeLog:

	PR fortran/103475
	* gfortran.dg/pr103475.f90: New test.
---
 gcc/fortran/primary.cc                 |  2 +-
 gcc/testsuite/gfortran.dg/pr103475.f90 | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr103475.f90

diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
index 28ce5fea865..1bea17d44fe 100644
--- a/gcc/fortran/primary.cc
+++ b/gcc/fortran/primary.cc
@@ -2770,7 +2770,7 @@ gfc_expr_attr (gfc_expr *e)
 	{
 	  gfc_symbol *sym = e->value.function.esym->result;
 	  attr = sym->attr;
-	  if (sym->ts.type == BT_CLASS)
+	  if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
 	    {
 	      attr.dimension = CLASS_DATA (sym)->attr.dimension;
 	      attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
diff --git a/gcc/testsuite/gfortran.dg/pr103475.f90 b/gcc/testsuite/gfortran.dg/pr103475.f90
new file mode 100644
index 00000000000..6cce5e8ebf7
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr103475.f90
@@ -0,0 +1,11 @@
+! { dg-do compile }
+! { dg-options "-O2 -Wall" }
+! PR fortran/103475 - ICE in gfc_expr_attr
+! Contributed by G.Steinmetz
+
+program p
+  type t
+  end type
+  class(t) :: x ! { dg-error "must be dummy, allocatable or pointer" }
+  y = x()       ! { dg-error "Cannot convert invalid class" }
+end
--
2.35.3


                 reply	other threads:[~2023-02-13 21:13 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-4f91f3ff-1a0f-441e-ae8b-8beffd701aa9-1676322798018@3c-app-gmx-bap61 \
    --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).