https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127991
Bug ID: 127991
Summary: Tree dumps crash when printing an anonymous Modula-2
set constant
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: ice-on-valid-code
Severity: normal
Priority: P3
Component: modula2
Assignee: gaius at gcc dot gnu.org
Reporter: yqtian668 at gmail dot com
Target Milestone: ---
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Build: x86_64-pc-linux-gnu
Created attachment 65889
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65889&action=edit
patch and test
The following valid Modula-2 program makes `cc1gm2` crash when a tree dump is
requested:
```modula2
MODULE source ;
TYPE
large = SET OF [0..1023] ;
PROCEDURE Check (a: large) ;
END Check ;
BEGIN
Check(large{2, 3, 4})
END source.
```
Command:
```text
gm2 -O0 -fdump-tree-optimized -S source.mod
```
Tested with GCC 17.0.0 20261018 (experimental), revision
2d1f1bc18a59c0f106c551304ac459ad7781a711, configured for
x86_64-pc-linux-gnu. A checking build reports an internal compiler error in
`output_buffer_append_r`; a non-checking build segfaults while printing the
tree. `-O0 -g -fcompare-debug` can reach the same failure. Compiling without
a tree-dump option succeeds.
`m2decl_DeclareKnownConstant` creates an `IDENTIFIER_NODE` directly in order
to ignore the constant's name. The node is non-null, but its identifier
string is null. When the anonymous set constant remains as a `CONST_DECL`
argument, `dump_decl_name` treats it as named and `pp_tree_identifier` tries
to print the null string.
A candidate patch builds the `CONST_DECL` with a `NULL_TREE` name instead.
The dumper then uses GCC's normal generated form such as `C.<uid>`. The
regression test requests the optimized dump and checks that the formerly
crashing anonymous constant is actually printed, so it cannot pass merely by
avoiding that path.
The focused test reports two expected passes. Complete `check-gm2` testing
on x86_64-pc-linux-gnu changes from 15,455 expected passes and no failures to
15,457 expected passes and no failures; the two additional passes are the new
test. `git diff --check` and `contrib/check_GNU_style.sh` are clean.
Note that this bug is found by our non-AI-based automated testing technique,
and then verfied by me with the assistnat of Chatgpt.
Any suggestion would be apprecaited. If it looks good to you, I am happy submit
the patch to gcc-patches