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

2023-11-29 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik closed https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-29 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik updated 
https://github.com/llvm/llvm-project/pull/73691

>From 21d6bbdd1f8676e51b053ec3dd9020270e6b929e Mon Sep 17 00:00:00 2001
From: Shafik Yaghmour 
Date: Tue, 28 Nov 2023 11:20:12 -0800
Subject: [PATCH] [Clang][Lex] Fix parsing of nested requirement to prevent
 flowing off 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: https://github.com/llvm/llvm-project/issues/73112
---
 clang/docs/ReleaseNotes.rst| 3 +++
 clang/lib/Parse/ParseExprCXX.cpp   | 6 --
 clang/test/Parser/cxx2a-concepts-requires-expr.cpp | 8 
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7c909ac3cab6419..f8abc44743da09c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -783,6 +783,9 @@ Bug Fixes to C++ Support
   completes (except deduction guides). Fixes:
   (`#59827 `_)
 
+- Fix crash when parsing nested requirement. Fixes:
+  (`#73112 `_)
+
 Bug Fixes to AST Handling
 ^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 79db094e098f8e6..8b86db1bb8fc5d5 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -3635,10 +3635,12 @@ ExprResult Parser::ParseRequiresExpression() {
   auto Res = TryParseParameterDeclarationClause();
   if (Res != TPResult::False) {
 // Skip to the closing parenthesis
-// FIXME: Don't traverse these tokens twice (here and in
-//  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 
 requires requires {
  typename BitInt; // ok
 } using r44 = void;
+
+namespace GH73112 {
+void f() {
+requires { requires(int; } // expected-error {{expected ')'}} \
+   // expected-error {{expected expression}} \
+   // expected-note {{to match this '('}}
+}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

Missing a release note, otherwise LGTM

https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread via cfe-commits


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

cor3ntin wrote:

Yeah, I think we can remove that 

https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread via cfe-commits

https://github.com/cor3ntin edited 
https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread Luís Ferreira via cfe-commits

https://github.com/ljmf00 approved this pull request.


https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread Shafik Yaghmour via cfe-commits


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

shafik wrote:

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(...)`

https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread via cfe-commits

llvmbot wrote:




@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: https://github.com/llvm/llvm-project/issues/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
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 
 requires requires {
  typename BitInt; // ok
 } using r44 = void;
+
+namespace GH73112 {
+void f() {
+requires { requires(int; } // expected-error {{expected ')'}} \
+   // expected-error {{expected expression}} \
+   // expected-note {{to match this '('}}
+}
+}

``




https://github.com/llvm/llvm-project/pull/73691
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik created 
https://github.com/llvm/llvm-project/pull/73691

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: https://github.com/llvm/llvm-project/issues/73112

>From 3f8b552ccc78c0d5ca98ed529ce28ac8c95f0656 Mon Sep 17 00:00:00 2001
From: Shafik Yaghmour 
Date: Tue, 28 Nov 2023 11:20:12 -0800
Subject: [PATCH] [Clang][Lex] Fix parsing of nested requirement to prevent
 flowing off 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: https://github.com/llvm/llvm-project/issues/73112
---
 clang/lib/Parse/ParseExprCXX.cpp   | 4 
 clang/test/Parser/cxx2a-concepts-requires-expr.cpp | 8 
 2 files changed, 12 insertions(+)

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 
 requires requires {
  typename BitInt; // ok
 } using r44 = void;
+
+namespace GH73112 {
+void f() {
+requires { requires(int; } // expected-error {{expected ')'}} \
+   // expected-error {{expected expression}} \
+   // expected-note {{to match this '('}}
+}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits