[clang] [ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (PR #108689)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 14 01:35:13 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Congcong Cai (HerrCai0907)
<details>
<summary>Changes</summary>
Fixes: #<!-- -->106444
---
Full diff: https://github.com/llvm/llvm-project/pull/108689.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/lib/ASTMatchers/ASTMatchFinder.cpp (+3-2)
- (modified) clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp (+13)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 79b154ef1aef5e..a6f4b4e602d571 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -504,6 +504,8 @@ AST Matchers
- Fixed an ordering issue with the `hasOperands` matcher occuring 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.
+
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; }; }",
``````````
</details>
https://github.com/llvm/llvm-project/pull/108689
More information about the cfe-commits
mailing list