Skip to content

Commit a849f6c

Browse files
committed
gccrs: fix segfault in clone_pattern w macro
Check if parser throw an error to avoid cloning nullptr Fixes #4140 gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (transcribe_expression): check if parser didn't fail (transcribe_type): check if parser didn't fail (transcribe_pattern): check if parser didn't fail Signed-off-by: lenny.chiadmi-delage <lenny.chiadmi-delage@epita.fr>
1 parent b92a684 commit a849f6c

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,10 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
962962

963963
auto attrs = parser.parse_outer_attributes ();
964964
auto expr = parser.parse_expr (std::move (attrs));
965-
if (expr == nullptr)
966-
{
967-
for (auto error : parser.get_errors ())
968-
error.emit ();
969-
return AST::Fragment::create_error ();
970-
}
965+
for (auto error : parser.get_errors ())
966+
error.emit ();
967+
if (!expr)
968+
return AST::Fragment::create_error ();
971969

972970
// FIXME: make this an error for some edititons
973971
if (parser.peek_current_token ()->get_id () == SEMICOLON)
@@ -997,6 +995,8 @@ transcribe_type (Parser<MacroInvocLexer> &parser)
997995
auto type = parser.parse_type (true);
998996
for (auto err : parser.get_errors ())
999997
err.emit ();
998+
if (!type)
999+
return AST::Fragment::create_error ();
10001000

10011001
auto end = lexer.get_offs ();
10021002

@@ -1018,6 +1018,9 @@ transcribe_pattern (Parser<MacroInvocLexer> &parser)
10181018
for (auto err : parser.get_errors ())
10191019
err.emit ();
10201020

1021+
if (!pattern)
1022+
return AST::Fragment::create_error ();
1023+
10211024
auto end = lexer.get_offs ();
10221025

10231026
return AST::Fragment ({std::move (pattern)},

0 commit comments

Comments
 (0)