]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Fix use-after-free of replaced friend instantiation [PR118807]
authorNathaniel Shead <nathanieloshead@gmail.com>
Mon, 10 Feb 2025 11:15:30 +0000 (22:15 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 11 Feb 2025 11:26:52 +0000 (22:26 +1100)
commitef83fae50d8f085fe8440bfa595875a2e2329871
treecf2c81c3f50f24bf30a0c558e1c3b7eeb40c0441
parent7317fc0b03380a83ad03a5fc4fabef5f38c44c9d
c++: Fix use-after-free of replaced friend instantiation [PR118807]

When instantiating a friend function, we call register_specialization
which adds it to the DECL_TEMPLATE_INSTANTIATIONS of the template.
However, in some circumstances we might immediately call pushdecl and
find an existing specialisation.  In this case, when reregistering the
specialisation we also need to update the DECL_TEMPLATE_INSTANTIATIONS
list so that we don't try to access the freed spec again later.

PR c++/118807

gcc/cp/ChangeLog:

* pt.cc (reregister_specialization): Remove spec from
DECL_TEMPLATE_INSTANTIATIONS.

gcc/testsuite/ChangeLog:

* g++.dg/modules/pr118807.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/pt.cc
gcc/testsuite/g++.dg/modules/pr118807.C [new file with mode: 0644]