Skip to content

Commit 6780d53

Browse files
FabianWolffPiotrZSL
authored andcommitted
[clang-tidy] Warn about arrays in bugprone-undefined-memory-manipulation
Adds basic support for arrays to non-trivial types. Fixes #55579 Differential Revision: https://reviews.llvm.org/D127036
1 parent 08b10af commit 6780d53

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ AST_MATCHER(CXXRecordDecl, isNotTriviallyCopyable) {
2222
} // namespace
2323

2424
void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) {
25-
const auto NotTriviallyCopyableObject =
26-
hasType(ast_matchers::hasCanonicalType(
27-
pointsTo(cxxRecordDecl(isNotTriviallyCopyable()))));
25+
const auto HasNotTriviallyCopyableDecl =
26+
hasDeclaration(cxxRecordDecl(isNotTriviallyCopyable()));
27+
const auto ArrayOfNotTriviallyCopyable =
28+
arrayType(hasElementType(HasNotTriviallyCopyableDecl));
29+
const auto NotTriviallyCopyableObject = hasType(hasCanonicalType(
30+
anyOf(pointsTo(qualType(anyOf(HasNotTriviallyCopyableDecl,
31+
ArrayOfNotTriviallyCopyable))),
32+
ArrayOfNotTriviallyCopyable)));
2833

2934
// Check whether destination object is not TriviallyCopyable.
3035
// Applicable to all three memory manipulation functions.

clang-tools-extra/test/clang-tidy/checkers/bugprone/undefined-memory-manipulation.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ void notTriviallyCopyable() {
126126
::memmove(&p, &vb, sizeof(int));
127127
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, source object type 'types::VirtualBase'
128128

129+
types::Copy ca[10];
130+
memset(ca, 0, sizeof(ca));
131+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::Copy[10]'
132+
memset(&ca, 0, sizeof(ca));
133+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::Copy[10]'
134+
129135
#define MEMSET memset(&vf, 0, sizeof(int));
130136
MEMSET
131137
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::VirtualFunc'
@@ -159,6 +165,17 @@ void notTriviallyCopyable() {
159165
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'aliases::Copy2'
160166
memset(pc3, 0, sizeof(int));
161167
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3'
168+
using Copy3Arr = Copy3[5];
169+
Copy3Arr c3a;
170+
memset(c3a, 0, sizeof(c3a));
171+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3Arr'
172+
memset(&c3a, 0, sizeof(c3a));
173+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3Arr'
174+
175+
typedef Copy3 Copy3Arr2[5];
176+
Copy3Arr2 c3a2;
177+
memset(c3a2, 0, sizeof(c3a2));
178+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3Arr2'
162179
}
163180

164181
void triviallyCopyable() {

0 commit comments

Comments
 (0)