gccrs: fix ICE on missing closing paren
authorMarc Poulhiès <dkm@kataplop.net>
Fri, 7 Oct 2022 16:48:36 +0000 (18:48 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 21 Feb 2023 11:36:33 +0000 (12:36 +0100)
Fix crash (segfault) on a missing closing parenthesis when parsing the
expressions in a block. The returned `expr` was missing a check before being
used.

Add corresponding test.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::parse_stmt_or_expr_without_block):
Check if `expr` is valid after parsing it.

gcc/testsuite/ChangeLog:

* rust/compile/missing_closing_paren.rs: New test.

gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/missing_closing_paren.rs [new file with mode: 0644]

index 72207a1..a4a912f 100644 (file)
@@ -11738,10 +11738,17 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
            // must be expression statement
            lexer.skip_token ();
 
-           std::unique_ptr<AST::ExprStmtWithoutBlock> stmt (
-             new AST::ExprStmtWithoutBlock (std::move (expr),
-                                            t->get_locus ()));
-           return ExprOrStmt (std::move (stmt));
+           if (expr)
+             {
+               std::unique_ptr<AST::ExprStmtWithoutBlock> stmt (
+                 new AST::ExprStmtWithoutBlock (std::move (expr),
+                                                t->get_locus ()));
+               return ExprOrStmt (std::move (stmt));
+             }
+           else
+             {
+               return ExprOrStmt::create_error ();
+             }
          }
 
        // return expression
diff --git a/gcc/testsuite/rust/compile/missing_closing_paren.rs b/gcc/testsuite/rust/compile/missing_closing_paren.rs
new file mode 100644 (file)
index 0000000..895c313
--- /dev/null
@@ -0,0 +1,3 @@
+fn foo() {
+    (""; // { dg-error "unexpected token .*" }
+}