xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-memset-bad-sizeof.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wno-sizeof-array-argument %s
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc extern "C" void *memset(void *, int, unsigned);
4*f4a2713aSLionel Sambuc extern "C" void *memmove(void *s1, const void *s2, unsigned n);
5*f4a2713aSLionel Sambuc extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
6*f4a2713aSLionel Sambuc extern "C" void *memcmp(void *s1, const void *s2, unsigned n);
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc struct S {int a, b, c, d;};
9*f4a2713aSLionel Sambuc typedef S* PS;
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc struct Foo {};
12*f4a2713aSLionel Sambuc typedef const Foo& CFooRef;
13*f4a2713aSLionel Sambuc typedef const Foo CFoo;
14*f4a2713aSLionel Sambuc typedef volatile Foo VFoo;
15*f4a2713aSLionel Sambuc typedef const volatile Foo CVFoo;
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc typedef double Mat[4][4];
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc template <class Dest, class Source>
bit_cast(const Source & source)20*f4a2713aSLionel Sambuc inline Dest bit_cast(const Source& source) {
21*f4a2713aSLionel Sambuc   Dest dest;
22*f4a2713aSLionel Sambuc   memcpy(&dest, &source, sizeof(dest));
23*f4a2713aSLionel Sambuc   return dest;
24*f4a2713aSLionel Sambuc }
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc // http://www.lysator.liu.se/c/c-faq/c-2.html#2-6
f(Mat m,const Foo & const_foo,char * buffer)27*f4a2713aSLionel Sambuc void f(Mat m, const Foo& const_foo, char *buffer) {
28*f4a2713aSLionel Sambuc   S s;
29*f4a2713aSLionel Sambuc   S* ps = &s;
30*f4a2713aSLionel Sambuc   PS ps2 = &s;
31*f4a2713aSLionel Sambuc   char arr[5];
32*f4a2713aSLionel Sambuc   char* parr[5];
33*f4a2713aSLionel Sambuc   Foo foo;
34*f4a2713aSLionel Sambuc   char* heap_buffer = new char[42];
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   /* Should warn */
37*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(&s));  // \
38*f4a2713aSLionel Sambuc       // expected-warning {{'memset' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to remove the addressof in the argument to 'sizeof' (and multiply it by the number of elements)?}}
39*f4a2713aSLionel Sambuc   memset(ps, 0, sizeof(ps));  // \
40*f4a2713aSLionel Sambuc       // expected-warning {{'memset' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?}}
41*f4a2713aSLionel Sambuc   memset(ps2, 0, sizeof(ps2));  // \
42*f4a2713aSLionel Sambuc       // expected-warning {{'memset' call operates on objects of type 'S' while the size is based on a different type 'PS' (aka 'S *')}} expected-note{{did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?}}
43*f4a2713aSLionel Sambuc   memset(ps2, 0, sizeof(typeof(ps2)));  // \
44*f4a2713aSLionel Sambuc       // expected-warning {{argument to 'sizeof' in 'memset' call is the same pointer type}}
45*f4a2713aSLionel Sambuc   memset(ps2, 0, sizeof(PS));  // \
46*f4a2713aSLionel Sambuc       // expected-warning {{argument to 'sizeof' in 'memset' call is the same pointer type}}
47*f4a2713aSLionel Sambuc   memset(heap_buffer, 0, sizeof(heap_buffer));  // \
48*f4a2713aSLionel Sambuc       // expected-warning {{'memset' call operates on objects of type 'char' while the size is based on a different type 'char *'}} expected-note{{did you mean to provide an explicit length?}}
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc   memcpy(&s, 0, sizeof(&s));  // \
51*f4a2713aSLionel Sambuc       // expected-warning {{'memcpy' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to remove the addressof in the argument to 'sizeof' (and multiply it by the number of elements)?}}
52*f4a2713aSLionel Sambuc   memcpy(0, &s, sizeof(&s));  // \
53*f4a2713aSLionel Sambuc       // expected-warning {{'memcpy' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to remove the addressof in the argument to 'sizeof' (and multiply it by the number of elements)?}}
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc   memmove(ps, 0, sizeof(ps));  // \
56*f4a2713aSLionel Sambuc       // expected-warning {{'memmove' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?}}
57*f4a2713aSLionel Sambuc   memcmp(ps, 0, sizeof(ps));  // \
58*f4a2713aSLionel Sambuc       // expected-warning {{'memcmp' call operates on objects of type 'S' while the size is based on a different type 'S *'}} expected-note{{did you mean to dereference the argument to 'sizeof' (and multiply it by the number of elements)?}}
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   /* Shouldn't warn */
61*f4a2713aSLionel Sambuc   memset((void*)&s, 0, sizeof(&s));
62*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(s));
63*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(S));
64*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(const S));
65*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(volatile S));
66*f4a2713aSLionel Sambuc   memset(&s, 0, sizeof(volatile const S));
67*f4a2713aSLionel Sambuc   memset(&foo, 0, sizeof(CFoo));
68*f4a2713aSLionel Sambuc   memset(&foo, 0, sizeof(VFoo));
69*f4a2713aSLionel Sambuc   memset(&foo, 0, sizeof(CVFoo));
70*f4a2713aSLionel Sambuc   memset(ps, 0, sizeof(*ps));
71*f4a2713aSLionel Sambuc   memset(ps2, 0, sizeof(*ps2));
72*f4a2713aSLionel Sambuc   memset(ps2, 0, sizeof(typeof(*ps2)));
73*f4a2713aSLionel Sambuc   memset(arr, 0, sizeof(arr));
74*f4a2713aSLionel Sambuc   memset(parr, 0, sizeof(parr));
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc   memcpy(&foo, &const_foo, sizeof(Foo));
77*f4a2713aSLionel Sambuc   memcpy((void*)&s, 0, sizeof(&s));
78*f4a2713aSLionel Sambuc   memcpy(0, (void*)&s, sizeof(&s));
79*f4a2713aSLionel Sambuc   char *cptr;
80*f4a2713aSLionel Sambuc   memcpy(&cptr, buffer, sizeof(cptr));
81*f4a2713aSLionel Sambuc   memcpy((char*)&cptr, buffer, sizeof(cptr));
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc   CFooRef cfoo = foo;
84*f4a2713aSLionel Sambuc   memcpy(&foo, &cfoo, sizeof(Foo));
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc   memcpy(0, &arr, sizeof(arr));
87*f4a2713aSLionel Sambuc   typedef char Buff[8];
88*f4a2713aSLionel Sambuc   memcpy(0, &arr, sizeof(Buff));
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc   unsigned char* puc;
91*f4a2713aSLionel Sambuc   bit_cast<char*>(puc);
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc   float* pf;
94*f4a2713aSLionel Sambuc   bit_cast<int*>(pf);
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   int iarr[14];
97*f4a2713aSLionel Sambuc   memset(&iarr[0], 0, sizeof iarr);
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc   int* iparr[14];
100*f4a2713aSLionel Sambuc   memset(&iparr[0], 0, sizeof iparr);
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc   memset(m, 0, sizeof(Mat));
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc   // Copy to raw buffer shouldn't warn either
105*f4a2713aSLionel Sambuc   memcpy(&foo, &arr, sizeof(Foo));
106*f4a2713aSLionel Sambuc   memcpy(&arr, &foo, sizeof(Foo));
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc   // Shouldn't warn, and shouldn't crash either.
109*f4a2713aSLionel Sambuc   memset(({
110*f4a2713aSLionel Sambuc     if (0) {}
111*f4a2713aSLionel Sambuc     while (0) {}
112*f4a2713aSLionel Sambuc     for (;;) {}
113*f4a2713aSLionel Sambuc     &s;
114*f4a2713aSLionel Sambuc   }), 0, sizeof(s));
115*f4a2713aSLionel Sambuc }
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc namespace ns {
118*f4a2713aSLionel Sambuc void memset(void* s, char c, int n);
f(int * i)119*f4a2713aSLionel Sambuc void f(int* i) {
120*f4a2713aSLionel Sambuc   memset(i, 0, sizeof(i));
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc }
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc extern "C" int strncmp(const char *s1, const char *s2, unsigned n);
125*f4a2713aSLionel Sambuc extern "C" int strncasecmp(const char *s1, const char *s2, unsigned n);
126*f4a2713aSLionel Sambuc extern "C" char *strncpy(char *det, const char *src, unsigned n);
127*f4a2713aSLionel Sambuc extern "C" char *strncat(char *dst, const char *src, unsigned n);
128*f4a2713aSLionel Sambuc extern "C" char *strndup(const  char *src, unsigned n);
129*f4a2713aSLionel Sambuc 
strcpy_and_friends()130*f4a2713aSLionel Sambuc void strcpy_and_friends() {
131*f4a2713aSLionel Sambuc   const char* FOO = "<- should be an array instead";
132*f4a2713aSLionel Sambuc   const char* BAR = "<- this, too";
133*f4a2713aSLionel Sambuc 
134*f4a2713aSLionel Sambuc   strncmp(FOO, BAR, sizeof(FOO)); // \
135*f4a2713aSLionel Sambuc       // expected-warning {{'strncmp' call operates on objects of type 'const char' while the size is based on a different type 'const char *'}} expected-note{{did you mean to provide an explicit length?}}
136*f4a2713aSLionel Sambuc   strncasecmp(FOO, BAR, sizeof(FOO));  // \
137*f4a2713aSLionel Sambuc       // expected-warning {{'strncasecmp' call operates on objects of type 'const char' while the size is based on a different type 'const char *'}} expected-note{{did you mean to provide an explicit length?}}
138*f4a2713aSLionel Sambuc 
139*f4a2713aSLionel Sambuc   char buff[80];
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc   strncpy(buff, BAR, sizeof(BAR)); // \
142*f4a2713aSLionel Sambuc       // expected-warning {{'strncpy' call operates on objects of type 'const char' while the size is based on a different type 'const char *'}} expected-note{{did you mean to provide an explicit length?}}
143*f4a2713aSLionel Sambuc   strndup(FOO, sizeof(FOO)); // \
144*f4a2713aSLionel Sambuc       // expected-warning {{'strndup' call operates on objects of type 'const char' while the size is based on a different type 'const char *'}} expected-note{{did you mean to provide an explicit length?}}
145*f4a2713aSLionel Sambuc }
146