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