Skip to content

Commit a03853b

Browse files
[clang] Warn about memset/memcpy to NonTriviallyCopyable types
This implements a warning that's similar to what GCC does in that context: both memcpy and memset require their first and second operand to be trivially copyable, let's warn if that's not the case.
1 parent 5621929 commit a03853b

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ Modified Compiler Flags
316316
to utilize these vector libraries. The behavior for all other vector function
317317
libraries remains unchanged.
318318

319+
- The ``-Wnontrivial-memaccess`` warning has been updated to also warn about
320+
passing non-trivially-copyable destrination parameter to ``memcpy``,
321+
``memset`` and similar functions for which it is a documented undefined
322+
behavior.
323+
319324
Removed Compiler Flags
320325
-------------------------
321326

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ def warn_cstruct_memaccess : Warning<
795795
"%1 call is a pointer to record %2 that is not trivial to "
796796
"%select{primitive-default-initialize|primitive-copy}3">,
797797
InGroup<NonTrivialMemaccess>;
798+
def warn_cxxstruct_memaccess : Warning<
799+
"first argument in call to "
800+
"%0 is a pointer to non-trivially copyable type %1">,
801+
InGroup<NonTrivialMemaccess>;
798802
def note_nontrivial_field : Note<
799803
"field is non-trivial to %select{copy|default-initialize}0">;
800804
def err_non_trivial_c_union_in_invalid_context : Error<

clang/lib/Sema/SemaChecking.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8899,18 +8899,36 @@ void Sema::CheckMemaccessArguments(const CallExpr *Call,
88998899
<< ArgIdx << FnName << PointeeTy
89008900
<< Call->getCallee()->getSourceRange());
89018901
else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
8902+
8903+
bool IsTriviallyCopyableCXXRecord =
8904+
RT->desugar().isTriviallyCopyableType(Context);
8905+
89028906
if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
89038907
RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
89048908
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
89058909
PDiag(diag::warn_cstruct_memaccess)
89068910
<< ArgIdx << FnName << PointeeTy << 0);
89078911
SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
8912+
} else if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
8913+
!IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
8914+
// FIXME: Limiting this warning to dest argument until we decide
8915+
// whether it's valid for source argument too.
8916+
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
8917+
PDiag(diag::warn_cxxstruct_memaccess)
8918+
<< FnName << PointeeTy);
89088919
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
89098920
RT->getDecl()->isNonTrivialToPrimitiveCopy()) {
89108921
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
89118922
PDiag(diag::warn_cstruct_memaccess)
89128923
<< ArgIdx << FnName << PointeeTy << 1);
89138924
SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
8925+
} else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
8926+
!IsTriviallyCopyableCXXRecord && ArgIdx == 0) {
8927+
// FIXME: Limiting this warning to dest argument until we decide
8928+
// whether it's valid for source argument too.
8929+
DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
8930+
PDiag(diag::warn_cxxstruct_memaccess)
8931+
<< FnName << PointeeTy);
89148932
} else {
89158933
continue;
89168934
}

clang/test/SemaCXX/constexpr-string.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ namespace MemcpyEtc {
670670
constexpr bool test_address_of_incomplete_struct_type() { // expected-error {{never produces a constant}}
671671
struct Incomplete;
672672
extern Incomplete x, y;
673+
// expected-warning@+2 {{first argument in call to '__builtin_memcpy' is a pointer to non-trivially copyable type 'Incomplete'}}
674+
// expected-note@+1 {{explicitly cast the pointer to silence this warning}}
673675
__builtin_memcpy(&x, &x, 4);
674676
// expected-note@-1 2{{cannot constant evaluate 'memcpy' between objects of incomplete type 'Incomplete'}}
675677
return true;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wnontrivial-memaccess %s
2+
3+
extern "C" void *bzero(void *, unsigned);
4+
extern "C" void *memset(void *, int, unsigned);
5+
extern "C" void *memmove(void *s1, const void *s2, unsigned n);
6+
extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
7+
8+
class TriviallyCopyable {};
9+
class NonTriviallyCopyable { NonTriviallyCopyable(const NonTriviallyCopyable&);};
10+
11+
void test_bzero(TriviallyCopyable* tc,
12+
NonTriviallyCopyable *ntc) {
13+
// OK
14+
bzero(tc, sizeof(*tc));
15+
16+
// expected-warning@+2{{first argument in call to 'bzero' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
17+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
18+
bzero(ntc, sizeof(*ntc));
19+
20+
// OK
21+
bzero((void*)ntc, sizeof(*ntc));
22+
}
23+
24+
void test_memset(TriviallyCopyable* tc,
25+
NonTriviallyCopyable *ntc) {
26+
// OK
27+
memset(tc, 0, sizeof(*tc));
28+
29+
// expected-warning@+2{{first argument in call to 'memset' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
30+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
31+
memset(ntc, 0, sizeof(*ntc));
32+
33+
// OK
34+
memset((void*)ntc, 0, sizeof(*ntc));
35+
}
36+
37+
38+
void test_memcpy(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
39+
NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
40+
// OK
41+
memcpy(tc0, tc1, sizeof(*tc0));
42+
43+
// expected-warning@+2{{first argument in call to 'memcpy' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
44+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
45+
memcpy(ntc0, ntc1, sizeof(*ntc0));
46+
47+
// ~ OK
48+
memcpy((void*)ntc0, ntc1, sizeof(*ntc0));
49+
50+
// OK
51+
memcpy((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
52+
}
53+
54+
void test_memmove(TriviallyCopyable* tc0, TriviallyCopyable* tc1,
55+
NonTriviallyCopyable *ntc0, NonTriviallyCopyable *ntc1) {
56+
// OK
57+
memmove(tc0, tc1, sizeof(*tc0));
58+
59+
// expected-warning@+2{{first argument in call to 'memmove' is a pointer to non-trivially copyable type 'NonTriviallyCopyable'}}
60+
// expected-note@+1{{explicitly cast the pointer to silence this warning}}
61+
memmove(ntc0, ntc1, sizeof(*ntc0));
62+
63+
// ~ OK
64+
memmove((void*)ntc0, ntc1, sizeof(*ntc0));
65+
66+
// OK
67+
memmove((void*)ntc0, (void*)ntc1, sizeof(*ntc0));
68+
}

0 commit comments

Comments
 (0)