Skip to content

[clang-tidy] Fix crash in modernize-use-designated-initializers check #113688

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 4 commits into from
Oct 29, 2024

Conversation

z1nke
Copy link
Contributor

@z1nke z1nke commented Oct 25, 2024

Fixed #113652
When calling Node.isAggregate() and Node.isPOD(), if Node is declared but not defined, it will result in null pointer dereference (and if assertions are enabled, it will cause an assertion failure).

@llvmbot
Copy link
Member

llvmbot commented Oct 25, 2024

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: None (z1nke)

Changes

Fixed #113652
When calling Node.isAggregate() and Node.isPOD(), if Node is declared but not defined, it will result in null pointer dereference (and if assertions are enabled, it will cause an assertion failure).


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

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp (+6-2)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp (+8)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
index 2a0cc403b726e8..3132067f3d5ece 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp
@@ -80,9 +80,13 @@ unsigned getNumberOfDesignated(const InitListExpr *SyntacticInitList) {
   });
 }
 
-AST_MATCHER(CXXRecordDecl, isAggregate) { return Node.isAggregate(); }
+AST_MATCHER(CXXRecordDecl, isAggregate) {
+  return Node.hasDefinition() && Node.isAggregate();
+}
 
-AST_MATCHER(CXXRecordDecl, isPOD) { return Node.isPOD(); }
+AST_MATCHER(CXXRecordDecl, isPOD) {
+  return Node.hasDefinition() && Node.isPOD();
+}
 
 AST_MATCHER(InitListExpr, isFullyDesignated) {
   if (const InitListExpr *SyntacticForm =
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp
index 9b769ad0be23ca..db1fe3baa99d7c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp
@@ -201,3 +201,11 @@ DECLARE_S93;
 // CHECK-MESSAGES-MACROS: :[[@LINE-1]]:1: warning: use designated initializer list to initialize 'S9' [modernize-use-designated-initializers]
 // CHECK-MESSAGES-MACROS: :[[@LINE-4]]:28: note: expanded from macro 'DECLARE_S93'
 // CHECK-MESSAGES-MACROS: :[[@LINE-71]]:1: note: aggregate type is defined here
+
+// Issus #113652.
+struct S14;
+
+struct S15{
+  S15(S14& d):d{d}{}
+  S14& d;
+};
\ No newline at end of file

@nicovank
Copy link
Contributor

I fixed a similar issue in 0aaac4f. There's an argument to be made whether this should be in the matchers (isPOD, isAggregate like here) versus an hasDefinition call in the parent before calling those matchers. I am slightly leaning towards the latter but this is not a strong opinion.

Add a small release notes entry?

@z1nke
Copy link
Contributor Author

z1nke commented Oct 26, 2024

@nicovank Thanks! Release note has been added.
What do others think about the argument?

Copy link
Contributor

@5chmidti 5chmidti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way is fine with me.

Copy link
Contributor

@nicovank nicovank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it again, this way is also good, no preference. Good to merge 👍

@z1nke
Copy link
Contributor Author

z1nke commented Oct 29, 2024

Hi, I don’t have write access to merge code, and this is my first commit to clang-tidy. What else needs to be done to merge this PR? Thanks!

@nicovank
Copy link
Contributor

@z1nke I will go ahead and click the merge button. Thanks!

@nicovank nicovank merged commit 27ef549 into llvm:main Oct 29, 2024
9 checks passed
@z1nke z1nke deleted the 113652-clang-tidy-crash-use-designated-init branch October 30, 2024 02:12
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…llvm#113688)

Fix llvm#113652.

When calling `Node.isAggregate()` and `Node.isPOD()`, if `Node` is declared but
not defined, it will result in null pointer dereference (and if assertions are
enabled, it will cause an assertion failure).
@mommsen
Copy link

mommsen commented Dec 9, 2024

Will this fix be added to an upcoming patch release for 19.1? We suffer from this issue using version 19.1.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] Crash when turn on the modernize-use-designated-initializers check.
6 participants