[1/2] Avoid potential memory leak in Rust parser

Message ID 20261028-rust-no-reset-v1-1-a57eab44c9a5@adacore.com
State New
Headers
Series Two small Rust parser changes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Tom Tromey Oct. 28, 2026, 6:49 p.m. UTC
  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(-)
  

Patch

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;