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