diff --git a/xls/dslx/frontend/parser.cc b/xls/dslx/frontend/parser.cc index 6dfc139041..5c6a403cad 100644 --- a/xls/dslx/frontend/parser.cc +++ b/xls/dslx/frontend/parser.cc @@ -1418,6 +1418,8 @@ absl::StatusOr Parser::ParseComparisonExpression( } absl::StatusOr Parser::ParsePattern(Bindings& bindings) { + XLS_ASSIGN_OR_RETURN(ExpressionDepthGuard depth_guard, BumpExpressionDepth()); + XLS_ASSIGN_OR_RETURN(std::optional oparen, TryPopToken(TokenKind::kOParen)); if (oparen.has_value()) { diff --git a/xls/dslx/frontend/parser_test.cc b/xls/dslx/frontend/parser_test.cc index c10adc2ddf..31ea0f9ead 100644 --- a/xls/dslx/frontend/parser_test.cc +++ b/xls/dslx/frontend/parser_test.cc @@ -3207,6 +3207,17 @@ TEST_F(ParserTest, UnreasonablyDeepTypeDefinition) { HasSubstr("Extremely deep nesting detected"))); } +// As above but guards the pattern-match production in the grammar. +TEST_F(ParserTest, UnreasonablyDeepPatternMatch) { + constexpr std::string_view kProgram = R"(fn f(x: u32) { match x { +((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()"; + Scanner s{file_table_, Fileno(0), std::string(kProgram)}; + Parser parser{"test", &s}; + absl::StatusOr> module = parser.ParseModule(); + EXPECT_THAT(module, StatusIs(absl::StatusCode::kInvalidArgument, + HasSubstr("Extremely deep nesting detected"))); +} + TEST_F(ParserTest, NonTypeDefinitionBeforeArrayLiteralColon) { constexpr std::string_view kProgram = "const A=4[5]:["; Scanner s{file_table_, Fileno(0), std::string(kProgram)};