c++/modules: Fix dangling pointer with imported_temploid_friends
commitec2365e07537e8b17745d75c28a2b45bf33be119
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 3 May 2024 09:36:17 +0000 (3 19:36 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 7 May 2024 01:37:47 +0000 (7 11:37 +1000)
tree370bdfd1a2943e9a7f8a521db213c7df37aa57bb
parentf56280d57fb6e2bb131f00ef66842f942cd0d01a
c++/modules: Fix dangling pointer with imported_temploid_friends

I got notified by Linaro CI and by checking testresults that there seems
to be some occasional failures in tpl-friend-4_b.C on some architectures
and standards modes since r15-59-gb5f6a56940e708.  I haven't been able
to reproduce but looking at the backtrace I suspect the issue is that
we're adding to the 'imported_temploid_friend' map a decl that is
ultimately discarded, which then has its address reused by a later decl
causing a failure in the assert in 'set_originating_module'.

This patch fixes the problem by ensuring 'imported_temploid_friends' is
correctly marked as a GTY root, and that 'duplicate_decls' properly
removes entries from the map for declarations that it frees.

PR c++/114275

gcc/cp/ChangeLog:

* cp-tree.h (remove_defining_module): Declare.
* decl.cc (duplicate_decls): Call remove_defining_module on
to-be-freed newdecl.
* module.cc (imported_temploid_friends): Mark as GTY root...
(init_modules): ...and allocate from ggc.
(trees_in::decl_value): Only track for declarations that won't
be discarded.
(remove_defining_module): New function.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
Reviewed-by: Patrick Palka <ppalka@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/module.cc