[clang] [ast-matcher] Fixed a crash when traverse lambda expr with invalid captures (PR #108689)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 16 19:03:04 PDT 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/108689
>From af8720ade42fef1571b59e2ca21943abc6b998d1 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 14 Sep 2024 15:38:11 +0800
Subject: [PATCH 1/2] [ast-matcher] Fixed a crash when traverse lambda expr
with invalid captures
Fixes: #106444
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/ASTMatchers/ASTMatchFinder.cpp | 5 +++--
.../ASTMatchers/ASTMatchersTraversalTest.cpp | 13 +++++++++++++
3 files changed, 18 insertions(+), 2 deletions(-)
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; }; }",
>From 3dd96ab7339c30c418c9298750426704eb14823f Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 17 Sep 2024 10:02:34 +0800
Subject: [PATCH 2/2] update release note
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a6f4b4e602d571..fbec5e7ace4e7d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -504,7 +504,7 @@ 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.
+- Fixed a crash when traverse lambda expr with invalid captures. (#GH106444)
clang-format
------------
More information about the cfe-commits
mailing list