-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference #146103
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
[clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference #146103
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Jonathan Marriott (JonathanMarriott) ChangesFixes #146101, in summary dumping a Diagnosed the cause by attaching the debugger to Full diff: https://github.com/llvm/llvm-project/pull/146103.diff 2 Files Affected:
diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index 8d02a50e2e8a5..8ebabb2bde10d 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -99,7 +99,7 @@ class ASTNodeTraverser
TraversalKind GetTraversalKind() const { return Traversal; }
void Visit(const Decl *D, bool VisitLocs = false) {
- if (Traversal == TK_IgnoreUnlessSpelledInSource && D->isImplicit())
+ if (Traversal == TK_IgnoreUnlessSpelledInSource && D && D->isImplicit())
return;
getNodeDelegate().AddChild([=] {
diff --git a/clang/unittests/AST/ASTTraverserTest.cpp b/clang/unittests/AST/ASTTraverserTest.cpp
index 8b6e3e90c0ea6..988e81d8e51de 100644
--- a/clang/unittests/AST/ASTTraverserTest.cpp
+++ b/clang/unittests/AST/ASTTraverserTest.cpp
@@ -28,6 +28,10 @@ class NodeTreePrinter : public TextTreeStructure {
: TextTreeStructure(OS, /* showColors */ false), OS(OS) {}
void Visit(const Decl *D) {
+ if (!D) {
+ OS << "<<<NULL>>>";
+ return;
+ }
OS << D->getDeclKindName() << "Decl";
if (auto *ND = dyn_cast<NamedDecl>(D)) {
OS << " '" << ND->getDeclName() << "'";
@@ -1932,4 +1936,75 @@ CXXRewrittenBinaryOperator
}
}
+TEST(Traverse, CatchStatements) {
+
+ auto AST = buildASTFromCode(R"cpp(
+void test()
+{
+ try
+ {
+ int a;
+ }
+ catch (...)
+ {
+ int b;
+ }
+
+ try
+ {
+ int a;
+ }
+ catch (const int&)
+ {
+ int b;
+ }
+}
+)cpp");
+
+ auto BN =
+ ast_matchers::match(cxxCatchStmt().bind("catch"), AST->getASTContext());
+ EXPECT_EQ(BN.size(), 2u);
+ const auto *catchWithoutDecl = BN[0].getNodeAs<Stmt>("catch");
+
+ llvm::StringRef Expected = R"cpp(
+CXXCatchStmt
+|-<<<NULL>>>
+`-CompoundStmt
+ `-DeclStmt
+ `-VarDecl 'b'
+)cpp";
+ EXPECT_EQ(dumpASTString(TK_AsIs, catchWithoutDecl), Expected);
+
+ Expected = R"cpp(
+CXXCatchStmt
+|-<<<NULL>>>
+`-CompoundStmt
+ `-DeclStmt
+ `-VarDecl 'b'
+)cpp";
+ EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, catchWithoutDecl),
+ Expected);
+
+ const auto *catchWithDecl = BN[1].getNodeAs<Stmt>("catch");
+
+ Expected = R"cpp(
+CXXCatchStmt
+|-VarDecl ''
+`-CompoundStmt
+ `-DeclStmt
+ `-VarDecl 'b'
+)cpp";
+ EXPECT_EQ(dumpASTString(TK_AsIs, catchWithDecl), Expected);
+
+ Expected = R"cpp(
+CXXCatchStmt
+|-VarDecl ''
+`-CompoundStmt
+ `-DeclStmt
+ `-VarDecl 'b'
+)cpp";
+ EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, catchWithDecl),
+ Expected);
+}
+
} // namespace clang
|
Thank you for the patch! Please add a release note to "clang/docs/ReleaseNotes.rst" so users can know the improvement. Please add a tag to the beginning of the PR title in square brackets like other PRs. E.g. |
Thanks, I've added a release note in what I think is the appropriate section! |
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, thank you for the fix! Once precommit CI comes back green, I can land the changes on your behalf.
@JonathanMarriott Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…reference (llvm#146103) In summary dumping a `catch(...)` statement using IgnoreUnlessSpelledInSource AST traversal causes a seg fault, as the variable declaration of the catch is `nullptr`. Diagnosed the cause by attaching the debugger to `clang-query`, this PR adds a fix to check for `nullptr` before accessing the `isImplicit()` method of the `Decl` pointee in the AST node traverser visitor Fixes llvm#146101
…reference (llvm#146103) In summary dumping a `catch(...)` statement using IgnoreUnlessSpelledInSource AST traversal causes a seg fault, as the variable declaration of the catch is `nullptr`. Diagnosed the cause by attaching the debugger to `clang-query`, this PR adds a fix to check for `nullptr` before accessing the `isImplicit()` method of the `Decl` pointee in the AST node traverser visitor Fixes llvm#146101
Fixes #146101, in summary dumping a
catch(...)
statement using IgnoreUnlessSpelledInSource AST traversal causes a seg fault, as the variable declaration of the catch isnullptr
.Diagnosed the cause by attaching the debugger to
clang-query
, this PR adds a fix to check fornullptr
before accessing theisImplicit()
method of theDecl
pointee in the AST node traverser visitor