From eae29a9bd05e34e28129efd5078baab5878c0b87 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Tue, 10 Oct 2023 17:46:28 -0400 Subject: [PATCH] memory leaks are all you need --- gcc/rust/ast/rust-macro.h | 5 +++++ gcc/rust/util/rust-hir-map.cc | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 79c1ce54161c..ecf168c9d9f4 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -576,6 +576,11 @@ class MacroRulesDefinition : public VisItem, public LocatedImpl MacroKind get_kind () const { return kind; } + std::unique_ptr clone_macro_rules_def () const + { + return std::unique_ptr (clone_item_impl ()); + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index a15d310ce6d2..1f76ef77574d 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -917,7 +917,8 @@ Mappings::insert_macro_invocation (AST::MacroInvocation &invoc, auto it = macroInvocations.find (invoc.get_macro_node_id ()); rust_assert (it == macroInvocations.end ()); - macroInvocations[invoc.get_macro_node_id ()] = def; + // TODO: remove hack that converts use-after-free into memory leak + macroInvocations[invoc.get_macro_node_id ()] = def->clone_macro_rules_def ().release (); } bool