Skip to content

[Clang][Lex] Fix parsing of nested requirement to prevent flowing off the end of token stream #73691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented Nov 28, 2023

Currently when parsing a nested requirement we attempt to balance parens if we have a parameter list. This will fail in some cases of ill-formed code and keep going until we fall off the token stream and crash. This fixes the hand parsing by using SkipUntil which will properly flag if we don't find the expected tokens.

Fixes: #73112

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 28, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 28, 2023

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Currently when parsing a nested requirement we attempt to balance parens if we have a parameter list. This will fail in some cases of ill-formed code and keep going until we fall off the token stream and crash. This fixes the hand parsing by using SkipUntil which will properly flag if we don't find the expected tokens.

Fixes: #73112


Full diff: https://github.com/llvm/llvm-project/pull/73691.diff

2 Files Affected:

  • (modified) clang/lib/Parse/ParseExprCXX.cpp (+4)
  • (modified) clang/test/Parser/cxx2a-concepts-requires-expr.cpp (+8)
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 79db094e098f8e6..a0903eb3a932490 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -3639,6 +3639,10 @@ ExprResult Parser::ParseRequiresExpression() {
                 //  TryParseParameterDeclarationClause).
                 unsigned Depth = 1;
                 while (Depth != 0) {
+                  bool FoundParen = SkipUntil(tok::l_paren, tok::r_paren,
+                                              SkipUntilFlags::StopBeforeMatch);
+                  if (!FoundParen)
+                    break;
                   if (Tok.is(tok::l_paren))
                     Depth++;
                   else if (Tok.is(tok::r_paren))
diff --git a/clang/test/Parser/cxx2a-concepts-requires-expr.cpp b/clang/test/Parser/cxx2a-concepts-requires-expr.cpp
index a18a54c7fad0690..971591afb08dba2 100644
--- a/clang/test/Parser/cxx2a-concepts-requires-expr.cpp
+++ b/clang/test/Parser/cxx2a-concepts-requires-expr.cpp
@@ -160,3 +160,11 @@ template <int N>
 requires requires {
  typename BitInt<N>; // ok
 } using r44 = void;
+
+namespace GH73112 {
+void f() {
+    requires { requires(int; } // expected-error {{expected ')'}} \
+                               // expected-error {{expected expression}} \
+                               // expected-note {{to match this '('}}
+}
+}

@@ -3639,6 +3639,10 @@ ExprResult Parser::ParseRequiresExpression() {
// TryParseParameterDeclarationClause).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the above fixme above is needed, I don't think there is any other way to handle this, at least based on some of the other places TryParseParameterDeclarationClause() is used e.g. TryParseFunctionDeclarator(...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can remove that

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a release note, otherwise LGTM

@@ -3639,6 +3639,10 @@ ExprResult Parser::ParseRequiresExpression() {
// TryParseParameterDeclarationClause).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can remove that

… the end of token stream

Currently when parsing a nested requirement we attempt to balance parens if we
have a parameter list. This will fail in some cases of ill-formed code and keep
going until we fall off the token stream and crash. This fixes the hand parsing
by using SkipUntil which will properly flag if we don't find the expected
tokens.

Fixes: llvm#73112
@shafik shafik force-pushed the fixNestedRequiresParsingCrash branch from 3f8b552 to 21d6bbd Compare November 30, 2023 00:31
@shafik shafik merged commit 0233a13 into llvm:main Nov 30, 2023
@shafik shafik deleted the fixNestedRequiresParsingCrash branch December 2, 2023 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang: parsing segfault with wrongly formed requires nested expression
4 participants