Skip to content

Commit 21d6bbd

Browse files
committed
[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: #73112
1 parent f3d2a31 commit 21d6bbd

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,9 @@ Bug Fixes to C++ Support
783783
completes (except deduction guides). Fixes:
784784
(`#59827 <https://github.com/llvm/llvm-project/issues/59827>`_)
785785

786+
- Fix crash when parsing nested requirement. Fixes:
787+
(`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)
788+
786789
Bug Fixes to AST Handling
787790
^^^^^^^^^^^^^^^^^^^^^^^^^
788791
- Fixed an import failure of recursive friend class template.

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,10 +3635,12 @@ ExprResult Parser::ParseRequiresExpression() {
36353635
auto Res = TryParseParameterDeclarationClause();
36363636
if (Res != TPResult::False) {
36373637
// Skip to the closing parenthesis
3638-
// FIXME: Don't traverse these tokens twice (here and in
3639-
// TryParseParameterDeclarationClause).
36403638
unsigned Depth = 1;
36413639
while (Depth != 0) {
3640+
bool FoundParen = SkipUntil(tok::l_paren, tok::r_paren,
3641+
SkipUntilFlags::StopBeforeMatch);
3642+
if (!FoundParen)
3643+
break;
36423644
if (Tok.is(tok::l_paren))
36433645
Depth++;
36443646
else if (Tok.is(tok::r_paren))

clang/test/Parser/cxx2a-concepts-requires-expr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ template <int N>
160160
requires requires {
161161
typename BitInt<N>; // ok
162162
} using r44 = void;
163+
164+
namespace GH73112 {
165+
void f() {
166+
requires { requires(int; } // expected-error {{expected ')'}} \
167+
// expected-error {{expected expression}} \
168+
// expected-note {{to match this '('}}
169+
}
170+
}

0 commit comments

Comments
 (0)