]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: Fix memory corruption at peek_context
authorPhilip Herron <herron.philip@googlemail.com>
Mon, 17 Apr 2023 19:48:41 +0000 (20:48 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:34:12 +0000 (18:34 +0100)
When working in the resolve_operator_overload it was found that we got
memory corruption as method resolution will use the query system and
therefore resolve new methods and the current function context info will
change and due to the fact the peek_context interface returns a reference
to the element which was now safe from a vector which can change and all
you need is the current function context at that moment in time.

gcc/rust/ChangeLog:

* typecheck/rust-autoderef.cc: don't take a reference
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise
* typecheck/rust-hir-type-check.h: remove reference
* typecheck/rust-typecheck-context.cc (TypeCheckContext::pop_return_type): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-autoderef.cc
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/rust/typecheck/rust-hir-type-check.h
gcc/rust/typecheck/rust-typecheck-context.cc

index d4a21ce1a262a3f9fc95a6f79e09c06e7bd96e6f..adaf575b2c15c39f98ca58394b8670177edcc9ff 100644 (file)
@@ -164,7 +164,7 @@ resolve_operator_overload_fn (
   // handle the case where we are within the impl block for this
   // lang_item otherwise we end up with a recursive operator overload
   // such as the i32 operator overload trait
-  TypeCheckContextItem &fn_context = context->peek_context ();
+  TypeCheckContextItem fn_context = context->peek_context ();
   if (fn_context.get_type () == TypeCheckContextItem::ItemType::IMPL_ITEM)
     {
       auto &impl_item = fn_context.get_impl_item ();
index 6594068a1eba085e750baa373afb7854f2629b03..ce215e35322ed2dd5f56046debc2e004945e3e1b 100644 (file)
@@ -1457,7 +1457,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)
 void
 TypeCheckExpr::visit (HIR::ClosureExpr &expr)
 {
-  TypeCheckContextItem &current_context = context->peek_context ();
+  TypeCheckContextItem current_context = context->peek_context ();
   TyTy::FnType *current_context_fndecl = current_context.get_context_type ();
 
   HirId ref = expr.get_mappings ().get_hirid ();
@@ -1624,7 +1624,7 @@ TypeCheckExpr::resolve_operator_overload (
   // handle the case where we are within the impl block for this lang_item
   // otherwise we end up with a recursive operator overload such as the i32
   // operator overload trait
-  TypeCheckContextItem &fn_context = context->peek_context ();
+  TypeCheckContextItem fn_context = context->peek_context ();
   if (fn_context.get_type () == TypeCheckContextItem::ItemType::IMPL_ITEM)
     {
       auto &impl_item = fn_context.get_impl_item ();
index 427c56be29f3237d57000d715b9fae0325c2ed88..10aa3b32a9e00234c17bbae81ef2b09b3a9bd041 100644 (file)
@@ -90,7 +90,7 @@ public:
   bool lookup_type_by_node_id (NodeId ref, HirId *id);
 
   TyTy::BaseType *peek_return_type ();
-  TypeCheckContextItem &peek_context ();
+  TypeCheckContextItem peek_context ();
   void push_return_type (TypeCheckContextItem item,
                         TyTy::BaseType *return_type);
   void pop_return_type ();
index 7b2c96cdce289f55f27da5832d376a9d7a7a399a..dcf06098cc5345ab50b819ef28dfa05acf6d65b8 100644 (file)
@@ -157,7 +157,7 @@ TypeCheckContext::pop_return_type ()
   return_type_stack.pop_back ();
 }
 
-TypeCheckContextItem &
+TypeCheckContextItem
 TypeCheckContext::peek_context ()
 {
   rust_assert (!return_type_stack.empty ());
This page took 0.072684 seconds and 5 git commands to generate.