Fix crash in Rust expression parser

A user found that an array expression with just a single value (like
"[23]") caused the Rust expression parser to crash.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30410
This commit is contained in:
Tom Tromey
2023-05-01 10:10:29 -06:00
parent c819a3380f
commit 87c84f07a0
2 changed files with 3 additions and 2 deletions

View File

@@ -1190,7 +1190,7 @@ rust_parser::parse_array ()
result = make_operation<rust_array_operation> (std::move (expr),
std::move (rhs));
}
else if (current_token == ',')
else if (current_token == ',' || current_token == ']')
{
std::vector<operation_up> ops;
ops.push_back (std::move (expr));
@@ -1205,7 +1205,7 @@ rust_parser::parse_array ()
int len = ops.size () - 1;
result = make_operation<array_operation> (0, len, std::move (ops));
}
else if (current_token != ']')
else
error (_("',', ';', or ']' expected"));
require (']');

View File

@@ -113,6 +113,7 @@ gdb_test "ptype \[1,2,3,4\]" "type = \\\[i32; 4\\\]"
gdb_test "print \[mut 1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]"
gdb_test "print \[1,2 3" "',' or ']' expected"
gdb_test "print \[1 2" "',', ';', or ']' expected"
gdb_test "print \[23\]" " = \\\[23\\\]"
gdb_test "print b\"hi rust\"" " = b\"hi rust\""
# This isn't rusty syntax yet, but that's another bug -- this is just