[clang] 07e0b8a - [ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (#108689)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 17 05:12:04 PDT 2024
Author: Congcong Cai
Date: 2024-09-17T20:12:01+08:00
New Revision: 07e0b8a7717aecc1133a08bfe013b58fb4c596f5
URL: https://github.com/llvm/llvm-project/commit/07e0b8a7717aecc1133a08bfe013b58fb4c596f5
DIFF: https://github.com/llvm/llvm-project/commit/07e0b8a7717aecc1133a08bfe013b58fb4c596f5.diff
LOG: [ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (#108689)
Fixes: #106444
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 69b2aea52aa9d3..8228055a1d861a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -506,6 +506,8 @@ AST Matchers
- Fixed an ordering issue with the `hasOperands` matcher occurring when setting a
binding in the first matcher and using it in the second matcher.
+- Fixed a crash when traverse lambda expr with invalid captures. (#GH106444)
+
clang-format
------------
diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 0bac2ed63a927e..3d01a70395a9bb 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -285,12 +285,13 @@ class MatchChildASTVisitor
ScopedIncrement ScopedDepth(&CurrentDepth);
for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
- const auto *C = Node->capture_begin() + I;
+ const LambdaCapture *C = Node->capture_begin() + I;
if (!C->isExplicit())
continue;
if (Node->isInitCapture(C) && !match(*C->getCapturedVar()))
return false;
- if (!match(*Node->capture_init_begin()[I]))
+ const Expr *CIE = Node->capture_init_begin()[I];
+ if (CIE != nullptr && !match(*CIE))
return false;
}
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index 028392f499da3b..ec0be27774d8b2 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -5052,6 +5052,19 @@ TEST(ForEachConstructorInitializer, MatchesInitializers) {
cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer()))));
}
+TEST(LambdaCapture, InvalidLambdaCapture) {
+ // not crash
+ EXPECT_FALSE(matches(
+ R"(int main() {
+ struct A { A()=default; A(A const&)=delete; };
+ A a; [a]() -> void {}();
+ return 0;
+ })",
+ traverse(TK_IgnoreUnlessSpelledInSource,
+ lambdaExpr(has(lambdaCapture()))),
+ langCxx11OrLater()));
+}
+
TEST(ForEachLambdaCapture, MatchesCaptures) {
EXPECT_TRUE(matches(
"int main() { int x, y; auto f = [x, y]() { return x + y; }; }",
More information about the cfe-commits
mailing list