diff options
| author | Nico Weber <nicolasweber@gmx.de> | 2011-06-14 16:14:58 +0000 |
|---|---|---|
| committer | Nico Weber <nicolasweber@gmx.de> | 2011-06-14 16:14:58 +0000 |
| commit | c5e7386983419e1943c719be598c4a46b82a2284 (patch) | |
| tree | 518e6bda8ffd0a2e2f321f9fc42928c723830723 /clang/test/SemaCXX/warn-memset-bad-sizeof.cpp | |
| parent | 3aeaf9e4c1d2167d3f9ba2a9415914e3dde8bb95 (diff) | |
| download | bcm5719-llvm-c5e7386983419e1943c719be598c4a46b82a2284.tar.gz bcm5719-llvm-c5e7386983419e1943c719be598c4a46b82a2284.zip | |
Warn on memset(ptr, 0, sizeof(ptr)). Diagnostic wording by Jordy Rose.
llvm-svn: 132996
Diffstat (limited to 'clang/test/SemaCXX/warn-memset-bad-sizeof.cpp')
| -rw-r--r-- | clang/test/SemaCXX/warn-memset-bad-sizeof.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-memset-bad-sizeof.cpp b/clang/test/SemaCXX/warn-memset-bad-sizeof.cpp new file mode 100644 index 00000000000..c000b447738 --- /dev/null +++ b/clang/test/SemaCXX/warn-memset-bad-sizeof.cpp @@ -0,0 +1,97 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// +extern "C" void *memset(void *, int, unsigned); +extern "C" void *memmove(void *s1, const void *s2, unsigned n); +extern "C" void *memcpy(void *s1, const void *s2, unsigned n); + +struct S {int a, b, c, d;}; +typedef S* PS; + +struct Foo {}; +typedef const Foo& CFooRef; +typedef const Foo CFoo; +typedef volatile Foo VFoo; +typedef const volatile Foo CVFoo; + +typedef double Mat[4][4]; + +template <class Dest, class Source> +inline Dest bit_cast(const Source& source) { + Dest dest; + memcpy(&dest, &source, sizeof(dest)); + return dest; +} + +// 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) { + S s; + S* ps = &s; + PS ps2 = &s; + char arr[5]; + char* parr[5]; + Foo foo; + + /* Should warn */ + memset(&s, 0, sizeof(&s)); // \ + // expected-warning {{the argument to sizeof is pointer type 'S *', expected 'S' to match first argument to 'memset'}} + memset(ps, 0, sizeof(ps)); // \ + // expected-warning {{the argument to sizeof is pointer type 'S *', expected 'S' to match first argument to 'memset'}} + memset(ps2, 0, sizeof(ps2)); // \ + // expected-warning {{the argument to sizeof is pointer type 'PS' (aka 'S *'), expected 'S' to match first argument to 'memset'}} + memset(ps2, 0, sizeof(typeof(ps2))); // \ + // 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'}} + memcpy(0, &s, sizeof(&s)); // \ + // expected-warning {{the argument to sizeof is pointer type 'S *', expected 'S' to match second argument to 'memcpy'}} + + /* Shouldn't warn */ + memset((void*)&s, 0, sizeof(&s)); + memset(&s, 0, sizeof(s)); + memset(&s, 0, sizeof(S)); + memset(&s, 0, sizeof(const S)); + memset(&s, 0, sizeof(volatile S)); + memset(&s, 0, sizeof(volatile const S)); + memset(&foo, 0, sizeof(CFoo)); + memset(&foo, 0, sizeof(VFoo)); + memset(&foo, 0, sizeof(CVFoo)); + memset(ps, 0, sizeof(*ps)); + memset(ps2, 0, sizeof(*ps2)); + memset(ps2, 0, sizeof(typeof(*ps2))); + memset(arr, 0, sizeof(arr)); + memset(parr, 0, sizeof(parr)); + + memcpy(&foo, &const_foo, sizeof(Foo)); + memcpy((void*)&s, 0, sizeof(&s)); + memcpy(0, (void*)&s, sizeof(&s)); + + CFooRef cfoo = foo; + memcpy(&foo, &cfoo, sizeof(Foo)); + + memcpy(0, &arr, sizeof(arr)); + typedef char Buff[8]; + memcpy(0, &arr, sizeof(Buff)); + + unsigned char* puc; + bit_cast<char*>(puc); + + float* pf; + bit_cast<int*>(pf); + + int iarr[14]; + memset(&iarr[0], 0, sizeof iarr); + + int* iparr[14]; + memset(&iparr[0], 0, sizeof iparr); + + memset(m, 0, sizeof(Mat)); + + // Copy to raw buffer shouldn't warn either + memcpy(&foo, &arr, sizeof(Foo)); + memcpy(&arr, &foo, sizeof(Foo)); +} |

