Fortran: error recovery handling invalid CLASS variable [PR107899]
Commit Message
Dear all,
the attached obvious patch fixes a NULL pointer dereference
that occurs with an invalid CLASS argument to DEALLOCATE.
Regtested on x86_64-pc-linux-gnu.
Will commit soon unless there are comments.
Thanks,
Harald
Comments
Hi Harald,
It looks good to me. OK to commit.
Thanks
Paul
On Sat, 3 Dec 2022 at 18:27, Harald Anlauf via Fortran <fortran@gcc.gnu.org>
wrote:
> Dear all,
>
> the attached obvious patch fixes a NULL pointer dereference
> that occurs with an invalid CLASS argument to DEALLOCATE.
>
> Regtested on x86_64-pc-linux-gnu.
>
> Will commit soon unless there are comments.
>
> Thanks,
> Harald
>
>
From dfe7f1a06d78f6319ad81f5774dc43fe52e37f45 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sat, 3 Dec 2022 19:21:07 +0100
Subject: [PATCH] Fortran: error recovery handling invalid CLASS variable
[PR107899]
gcc/fortran/ChangeLog:
PR fortran/107899
* resolve.cc (resolve_deallocate_expr): Avoid NULL pointer dereference
on invalid CLASS variable.
gcc/testsuite/ChangeLog:
PR fortran/107899
* gfortran.dg/pr107899.f90: New test.
---
gcc/fortran/resolve.cc | 2 +-
gcc/testsuite/gfortran.dg/pr107899.f90 | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/pr107899.f90
@@ -7570,7 +7570,7 @@ resolve_deallocate_expr (gfc_expr *e)
sym = e->symtree->n.sym;
unlimited = UNLIMITED_POLY(sym);
- if (sym->ts.type == BT_CLASS)
+ if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
{
allocatable = CLASS_DATA (sym)->attr.allocatable;
pointer = CLASS_DATA (sym)->attr.class_pointer;
new file mode 100644
@@ -0,0 +1,13 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+! PR fortran/107899 - ICE in resolve_deallocate_expr
+! Contributed by G.Steinmetz
+
+program p
+ type t
+ end type
+ class(t), target :: x[:] ! { dg-error "deferred shape" }
+ if (allocated (x)) then ! { dg-error "must be ALLOCATABLE" }
+ deallocate (x) ! { dg-error "must be ALLOCATABLE or a POINTER" }
+ end if
+end
--
2.35.3