aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Sema/SemaChecking.cpp4
-rw-r--r--test/SemaCXX/warn-memset-bad-sizeof.cpp7
2 files changed, 7 insertions, 4 deletions
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index d45ebf9033..945964fc35 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -1866,7 +1866,9 @@ void Sema::CheckMemsetcpymoveArguments(const CallExpr *Call,
if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
QualType PointeeTy = DestPtrTy->getPointeeType();
- if (PointeeTy->isVoidType())
+ // Don't warn about void pointers or char pointers as both are often used
+ // for directly representing memory, regardless of its underlying type.
+ if (PointeeTy->isVoidType() || PointeeTy->isCharType())
continue;
// Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p).
diff --git a/test/SemaCXX/warn-memset-bad-sizeof.cpp b/test/SemaCXX/warn-memset-bad-sizeof.cpp
index 9221590bd9..167f05f3da 100644
--- a/test/SemaCXX/warn-memset-bad-sizeof.cpp
+++ b/test/SemaCXX/warn-memset-bad-sizeof.cpp
@@ -23,10 +23,11 @@ inline Dest bit_cast(const Source& source) {
}
// http://www.lysator.liu.se/c/c-faq/c-2.html#2-6
-void f(char fake_array[8], Mat m, const Foo& const_foo) {
+void f(Mat m, const Foo& const_foo) {
S s;
S* ps = &s;
PS ps2 = &s;
+ char c = 42;
char arr[5];
char* parr[5];
Foo foo;
@@ -42,8 +43,6 @@ void f(char fake_array[8], Mat m, const Foo& const_foo) {
// expected-warning {{the argument to sizeof is pointer type 'typeof (ps2)' (aka 'S *'), expected 'S' to match first argument to 'memset'}}
memset(ps2, 0, sizeof(PS)); // \
// expected-warning {{the argument to sizeof is pointer type 'PS' (aka 'S *'), expected 'S' to match first argument to 'memset'}}
- memset(fake_array, 0, sizeof(fake_array)); // \
- // expected-warning {{the argument to sizeof is pointer type 'char *', expected 'char' to match first argument to 'memset'}}
memcpy(&s, 0, sizeof(&s)); // \
// expected-warning {{the argument to sizeof is pointer type 'S *', expected 'S' to match first argument to 'memcpy'}}
@@ -69,6 +68,8 @@ void f(char fake_array[8], Mat m, const Foo& const_foo) {
memcpy(&foo, &const_foo, sizeof(Foo));
memcpy((void*)&s, 0, sizeof(&s));
memcpy(0, (void*)&s, sizeof(&s));
+ memcpy(&parr[3], &c, sizeof(&c));
+ memcpy((char*)&parr[3], &c, sizeof(&c));
CFooRef cfoo = foo;
memcpy(&foo, &cfoo, sizeof(Foo));