https://gcc.gnu.org/g:653d8174d31cedb44d8ef19777ce51e75b2c3df4

commit r14-12945-g653d8174d31cedb44d8ef19777ce51e75b2c3df4
Author: Harald Anlauf <[email protected]>
Date:   Fri Oct 2 23:37:46 2026 +0200

    fortran: Fix error recovery on invalid inquiry refs [PR127683]
    
    Commit r14-9489 added an assert that could trigger while parsing an inquiry
    ref.  Replace assertion by the return of a failure value.  Fix latent
    frontend memleaks on error return path.
    
            PR fortran/127683
    
    gcc/fortran/ChangeLog:
    
            * primary.cc (gfc_match_varspec): Fix two minor frontend memleaks.
            * symbol.cc (gfc_find_derived_types): Return 0 when sym->assoc is
            NULL.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr127683.f90: New test.
    
    (cherry picked from commit ab3c778a2530b25713002ba78af65334174deb3d)

Diff:
---
 gcc/fortran/primary.cc                 |  2 ++
 gcc/fortran/symbol.cc                  |  3 ++-
 gcc/testsuite/gfortran.dg/pr127683.f90 | 10 ++++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
index 9bff8139095f..6e1ac9f3caf9 100644
--- a/gcc/fortran/primary.cc
+++ b/gcc/fortran/primary.cc
@@ -2438,6 +2438,7 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, 
bool sub_flag,
                    {
                        gfc_error ("The RE or IM part_ref at %C must be "
                                   "applied to a COMPLEX expression");
+                       gfc_free_ref_list (tmp);
                        return MATCH_ERROR;
                    }
                  else if (tmp->u.i == INQUIRY_LEN
@@ -2445,6 +2446,7 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, 
bool sub_flag,
                    {
                        gfc_error ("The LEN part_ref at %C must be applied "
                                   "to a CHARACTER expression");
+                       gfc_free_ref_list (tmp);
                        return MATCH_ERROR;
                    }
                }
diff --git a/gcc/fortran/symbol.cc b/gcc/fortran/symbol.cc
index 422814e2eaca..5a4a9c658530 100644
--- a/gcc/fortran/symbol.cc
+++ b/gcc/fortran/symbol.cc
@@ -2448,7 +2448,8 @@ gfc_find_derived_types (gfc_symbol *sym, gfc_namespace 
*ns,
                        const char *name, bool stash)
 {
   gfc_namespace *encompassing = NULL;
-  gcc_assert (sym->assoc);
+  if (sym->assoc == NULL)
+    return 0;
 
   cts = 0;
   while (ns->parent)
diff --git a/gcc/testsuite/gfortran.dg/pr127683.f90 
b/gcc/testsuite/gfortran.dg/pr127683.f90
new file mode 100644
index 000000000000..8e31c87df3ec
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr127683.f90
@@ -0,0 +1,10 @@
+! { dg-do compile }
+!
+! PR fortran/127683 - we used to ICE on invalid inquiry refs
+
+program test
+  implicit none
+  print *, z%re   ! { dg-error "must be applied to a COMPLEX expression" }
+  print *, c%len  ! { dg-error "must be applied to a CHARACTER expression" }
+! print *, x%kind ! this also used to ICE, now gives a fatal error
+end program

Reply via email to