Skip to content

Commit 27ef549

Browse files
authored
[clang-tidy] Fix crash in modernize-use-designated-initializers check (#113688)
Fix #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).
1 parent 8a0cb9a commit 27ef549

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

clang-tools-extra/clang-tidy/modernize/UseDesignatedInitializersCheck.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ unsigned getNumberOfDesignated(const InitListExpr *SyntacticInitList) {
8080
});
8181
}
8282

83-
AST_MATCHER(CXXRecordDecl, isAggregate) { return Node.isAggregate(); }
83+
AST_MATCHER(CXXRecordDecl, isAggregate) {
84+
return Node.hasDefinition() && Node.isAggregate();
85+
}
8486

85-
AST_MATCHER(CXXRecordDecl, isPOD) { return Node.isPOD(); }
87+
AST_MATCHER(CXXRecordDecl, isPOD) {
88+
return Node.hasDefinition() && Node.isPOD();
89+
}
8690

8791
AST_MATCHER(InitListExpr, isFullyDesignated) {
8892
if (const InitListExpr *SyntacticForm =

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ Changes in existing checks
216216
a false positive when only an implicit conversion happened inside an
217217
initializer list.
218218

219+
- Improved :doc:`modernize-use-designated-initializers
220+
<clang-tidy/checks/modernize/use-designated-initializers>` check to fix a
221+
crash when a class is declared but not defined.
222+
219223
- Improved :doc:`modernize-use-nullptr
220224
<clang-tidy/checks/modernize/use-nullptr>` check to also recognize
221225
``NULL``/``__null`` (but not ``0``) when used with a templated type.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ DECLARE_S93;
201201
// CHECK-MESSAGES-MACROS: :[[@LINE-1]]:1: warning: use designated initializer list to initialize 'S9' [modernize-use-designated-initializers]
202202
// CHECK-MESSAGES-MACROS: :[[@LINE-4]]:28: note: expanded from macro 'DECLARE_S93'
203203
// CHECK-MESSAGES-MACROS: :[[@LINE-71]]:1: note: aggregate type is defined here
204+
205+
// Issue #113652.
206+
struct S14;
207+
208+
struct S15{
209+
S15(S14& d):d{d}{}
210+
S14& d;
211+
};

0 commit comments

Comments
 (0)