public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] gccrs: Fix memory corruption at peek_context
Date: Tue,  2 May 2023 07:10:03 +0000 (GMT)	[thread overview]
Message-ID: <20230502071003.0AF6A3857723@sourceware.org> (raw)

https://gcc.gnu.org/g:04fad308c62f619b6002f290dc72bcd9747a911c

commit 04fad308c62f619b6002f290dc72bcd9747a911c
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Mon Apr 17 20:48:41 2023 +0100

    gccrs: Fix memory corruption at peek_context
    
    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>

Diff:
---
 gcc/rust/typecheck/rust-autoderef.cc           | 2 +-
 gcc/rust/typecheck/rust-hir-type-check-expr.cc | 4 ++--
 gcc/rust/typecheck/rust-hir-type-check.h       | 2 +-
 gcc/rust/typecheck/rust-typecheck-context.cc   | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/typecheck/rust-autoderef.cc b/gcc/rust/typecheck/rust-autoderef.cc
index 90a67ed62f2..078a5705b56 100644
--- a/gcc/rust/typecheck/rust-autoderef.cc
+++ b/gcc/rust/typecheck/rust-autoderef.cc
@@ -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 ();
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index f8ac63266a8..f205a49e707 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -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 ();
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 62fd8f3ddee..ec63657aaa6 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -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 ();
diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc
index 8bca751e788..7a40fc8313b 100644
--- a/gcc/rust/typecheck/rust-typecheck-context.cc
+++ b/gcc/rust/typecheck/rust-typecheck-context.cc
@@ -157,7 +157,7 @@ TypeCheckContext::pop_return_type ()
   return_type_stack.pop_back ();
 }
 
-TypeCheckContextItem &
+TypeCheckContextItem
 TypeCheckContext::peek_context ()
 {
   rust_assert (!return_type_stack.empty ());

                 reply	other threads:[~2023-05-02  7:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230502071003.0AF6A3857723@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).