public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7456] Fortran: error recovery after invalid assumed type declaration
@ 2022-03-02 18:23 Harald Anlauf
  0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2022-03-02 18:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:12463f1ecbcb30b39b8212454a6e598645123305

commit r12-7456-g12463f1ecbcb30b39b8212454a6e598645123305
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Mar 1 23:13:17 2022 +0100

    Fortran: error recovery after invalid assumed type declaration
    
    gcc/fortran/ChangeLog:
    
            PR fortran/104573
            * resolve.cc (resolve_structure_cons): Avoid NULL pointer
            dereference when there is no valid component.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/104573
            * gfortran.dg/assumed_type_14.f90: New test.

Diff:
---
 gcc/fortran/resolve.cc                        | 10 +++++++---
 gcc/testsuite/gfortran.dg/assumed_type_14.f90 | 22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 753aa27e23f..0afa5d3346a 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -1288,15 +1288,19 @@ resolve_structure_cons (gfc_expr *expr, int init)
 	}
     }
 
-  cons = gfc_constructor_first (expr->value.constructor);
-
   /* A constructor may have references if it is the result of substituting a
      parameter variable.  In this case we just pull out the component we
      want.  */
   if (expr->ref)
     comp = expr->ref->u.c.sym->components;
-  else
+  else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
+	    || expr->ts.type == BT_UNION)
+	   && expr->ts.u.derived)
     comp = expr->ts.u.derived->components;
+  else
+    return false;
+
+  cons = gfc_constructor_first (expr->value.constructor);
 
   for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
     {
diff --git a/gcc/testsuite/gfortran.dg/assumed_type_14.f90 b/gcc/testsuite/gfortran.dg/assumed_type_14.f90
new file mode 100644
index 00000000000..112cde34b27
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/assumed_type_14.f90
@@ -0,0 +1,22 @@
+! { dg-do compile }
+! PR fortran/104573 - ICE in resolve_structure_cons
+! Contributed by G.Steinmetz
+! Contributed by M.Morin
+
+program p
+  type t
+  end type
+  type(*), parameter :: x = t() ! { dg-error "Assumed type of variable" }
+  print *, x
+end
+
+subroutine s
+  type t
+     integer :: a
+  end type
+  character(3), parameter :: x = t(2) ! { dg-error "Cannot convert" }
+  character(3), parameter :: y = x    ! { dg-error "Unclassifiable statement" }
+  print *, y
+end
+
+! { dg-prune-output "Cannot convert" }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-02 18:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-02 18:23 [gcc r12-7456] Fortran: error recovery after invalid assumed type declaration Harald Anlauf

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).