[clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference by JonathanMarriott · Pull Request #146103 · llvm/llvm-project (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@JonathanMarriott

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

@JonathanMarriott

@JonathanMarriott

@github-actions

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.

@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

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

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) {

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) {

+TEST(Traverse, CatchStatements) { +

+)cpp";

+)cpp";

+)cpp";

+)cpp";

+} + } // namespace clang

@JonathanMarriott

@zwuis

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 titleFix AST IgnoreUnlessSpelledInSource traversal nullptr dereference [clang] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference

Jun 27, 2025

@JonathanMarriott

@JonathanMarriott JonathanMarriott changed the title[clang] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference [clang][AST] Fix AST IgnoreUnlessSpelledInSource traversal nullptr dereference

Jun 27, 2025

@JonathanMarriott

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!

AaronBallman

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.

@github-actions

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

Labels

clang:frontend

Language frontend issues, e.g. anything involving "Sema"

clang

Clang issues not falling into any other category