[PATCH, committed] Fix ICE after rejected CHARACTER duplicate declaration [PR82721]

Jerry D <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
This fixes a regression.

The master branch has been updated by Jerry DeLisle <[email protected]>:

https://gcc.gnu.org/g:5cfaad50af7dc25f6174044cdc05ebd56b6c4e3c

commit r16-8023-g5cfaad50af7dc25f6174044cdc05ebd56b6c4e3c
Author: Christopher Albert <[email protected]>
Date:   Tue Mar 10 18:17:32 2026 +0100

     Fortran: Fix ICE after rejected CHARACTER duplicate declaration [PR82721]

     When a CHARACTER declaration is rejected because the symbol already has
     a different basic type, declaration parsing may already have created a
     fresh gfc_charlen node for the rejected entity.  reject_statement()
     undoes symbol-table changes, but it does not roll back the namespace
     charlen list, so the stale len(...) expression can survive and later be
     resolved through dangling symtree pointers, causing corrupted
     diagnostics or an ICE.

     Fix this in build_sym by discarding only the unattached gfc_charlen
     node created for the rejected declaration before it is ever attached to
     any surviving symbol.  Keep shared charlen nodes intact so other
     invalid-code diagnostics still see the state they expect.  Also add a
     regression test that uses MALLOC_PERTURB_ to make the old crash
     reproducible.

     gcc/fortran/ChangeLog:

             PR fortran/82721
             * decl.cc (discard_pending_charlen): New helper.
             (build_sym): Discard unattached CHARACTER length nodes when
             gfc_add_type rejects the declaration.

     gcc/testsuite/ChangeLog:

             PR fortran/82721
             * gfortran.dg/pr82721.f90: New test.

     Signed-off-by: Christopher Albert <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.