Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 1/2] Avoid potential memory leak in Rust parser
Date: Wed, 28 Oct 2026 12:49:33 -0600	[thread overview]
Message-ID: <20261028-rust-no-reset-v1-1-a57eab44c9a5@adacore.com> (raw)
In-Reply-To: <20261028-rust-no-reset-v1-0-a57eab44c9a5@adacore.com>

rust_parser::parse_field has some support for completion.  This code
allocates a rust_structop using plain 'new', then stores it somewhere.
However, between the allocation and the store, the function calls
"lex()", which might throw.  This would leak the rust_structop object.

This patch rearranges the code slightly to avoid the memory leak.
---
 gdb/rust-parse.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index db4122119a5..9aaf85dc81c 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -1544,17 +1544,18 @@ rust_parser::parse_field (operation_up &&lhs)
     case COMPLETE:
       {
 	bool is_complete = current_token == COMPLETE;
-	auto struct_op = new rust_structop (std::move (lhs),
-					    std::string (current_string_val));
+	auto struct_op
+	  = std::make_unique<rust_structop> (std::move (lhs),
+					     std::string (current_string_val));
 	lex ();
 	if (is_complete)
 	  {
-	    completion_op.reset (struct_op);
-	    pstate->mark_struct_expression (struct_op);
+	    pstate->mark_struct_expression (struct_op.get ());
+	    completion_op = std::move (struct_op);
 	    /* Throw to the outermost level of the parser.  */
 	    error (_("not really an error"));
 	  }
-	result.reset (struct_op);
+	result = std::move (struct_op);
       }
       break;
 

-- 
2.56.0


  reply	other threads:[~2026-10-28 18:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 18:49 [PATCH 0/2] Two small Rust parser changes Tom Tromey
2026-10-28 18:49 ` Tom Tromey [this message]
2026-10-28 18:49 ` [PATCH 2/2] Remove rust_parser::reset Tom Tromey

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=20261028-rust-no-reset-v1-1-a57eab44c9a5@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.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