Skip to content

[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

Merged
merged 3 commits into from
Jun 30, 2025

Conversation

JonathanMarriott
Copy link
Contributor

Fixes #146101, 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

Copy link

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 @ followed by their GitHub username.

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.

@JonathanMarriott JonathanMarriott marked this pull request as ready for review June 27, 2025 16:15
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2025

@llvm/pr-subscribers-clang

Author: Jonathan Marriott (JonathanMarriott)

Changes

Fixes #146101, 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


Full diff: https://github.com/llvm/llvm-project/pull/146103.diff

2 Files Affected:

  • (modified) clang/include/clang/AST/ASTNodeTraverser.h (+1-1)
  • (modified) clang/unittests/AST/ASTTraverserTest.cpp (+75)
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

@JonathanMarriott
Copy link
Contributor Author

CC: @AaronBallman @Sirraide

@zwuis
Copy link
Contributor

zwuis commented Jun 27, 2025

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. [clang].

@JonathanMarriott JonathanMarriott changed the title Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference [clang] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference Jun 27, 2025
@JonathanMarriott JonathanMarriott changed the title [clang] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference [clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference Jun 27, 2025
@JonathanMarriott
Copy link
Contributor Author

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. [clang].

Thanks, I've added a release note in what I think is the appropriate section!

Copy link
Collaborator

@AaronBallman AaronBallman left a 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.

@AaronBallman AaronBallman merged commit 44ec3e8 into llvm:main Jun 30, 2025
8 checks passed
Copy link

@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!

rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
…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
rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-query] Crash when dumping catch(...) stmts when using IgnoreUnlessSpelledInSource traversal
4 participants