-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ast-matcher] Fixed a crash when traverse lambda expr with invalid captures #108689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ast-matcher] Fixed a crash when traverse lambda expr with invalid captures #108689
Conversation
@llvm/pr-subscribers-clang Author: Congcong Cai (HerrCai0907) ChangesFixes: #106444 Full diff: https://github.com/llvm/llvm-project/pull/108689.diff 3 Files Affected:
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; }; }",
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM aside from a release note nit, thank you!
clang/docs/ReleaseNotes.rst
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mention the issue number in the release note (#GH106444
and it will get auto-replaced with a URL to the issue).
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/5646 Here is the relevant piece of the build log for the reference
|
Fixes: #106444