Skip to content

Commit 07e0b8a

Browse files
authoredSep 17, 2024··
[ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (#108689)
Fixes: #106444
1 parent d0438d2 commit 07e0b8a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed
 

‎clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ AST Matchers
506506
- Fixed an ordering issue with the `hasOperands` matcher occurring when setting a
507507
binding in the first matcher and using it in the second matcher.
508508

509+
- Fixed a crash when traverse lambda expr with invalid captures. (#GH106444)
510+
509511
clang-format
510512
------------
511513

‎clang/lib/ASTMatchers/ASTMatchFinder.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,13 @@ class MatchChildASTVisitor
285285
ScopedIncrement ScopedDepth(&CurrentDepth);
286286

287287
for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
288-
const auto *C = Node->capture_begin() + I;
288+
const LambdaCapture *C = Node->capture_begin() + I;
289289
if (!C->isExplicit())
290290
continue;
291291
if (Node->isInitCapture(C) && !match(*C->getCapturedVar()))
292292
return false;
293-
if (!match(*Node->capture_init_begin()[I]))
293+
const Expr *CIE = Node->capture_init_begin()[I];
294+
if (CIE != nullptr && !match(*CIE))
294295
return false;
295296
}
296297

‎clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -5052,6 +5052,19 @@ TEST(ForEachConstructorInitializer, MatchesInitializers) {
50525052
cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer()))));
50535053
}
50545054

5055+
TEST(LambdaCapture, InvalidLambdaCapture) {
5056+
// not crash
5057+
EXPECT_FALSE(matches(
5058+
R"(int main() {
5059+
struct A { A()=default; A(A const&)=delete; };
5060+
A a; [a]() -> void {}();
5061+
return 0;
5062+
})",
5063+
traverse(TK_IgnoreUnlessSpelledInSource,
5064+
lambdaExpr(has(lambdaCapture()))),
5065+
langCxx11OrLater()));
5066+
}
5067+
50555068
TEST(ForEachLambdaCapture, MatchesCaptures) {
50565069
EXPECT_TRUE(matches(
50575070
"int main() { int x, y; auto f = [x, y]() { return x + y; }; }",

0 commit comments

Comments
 (0)
Please sign in to comment.