blob: e388634e8840ca0e35bd0546ed2fa0109dc72a99 [file] [log] [blame]
Nico Weber55080a72011-06-15 04:50:13 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wno-sizeof-array-argument %s
Nico Webere4a1c642011-06-14 16:14:58 +00002//
3extern "C" void *memset(void *, int, unsigned);
4extern "C" void *memmove(void *s1, const void *s2, unsigned n);
5extern "C" void *memcpy(void *s1, const void *s2, unsigned n);
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00006extern "C" void *memcmp(void *s1, const void *s2, unsigned n);
Nico Webere4a1c642011-06-14 16:14:58 +00007
8struct S {int a, b, c, d;};
9typedef S* PS;
10
11struct Foo {};
12typedef const Foo& CFooRef;
13typedef const Foo CFoo;
14typedef volatile Foo VFoo;
15typedef const volatile Foo CVFoo;
16
17typedef double Mat[4][4];
18
19template <class Dest, class Source>
20inline 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
Chandler Carruthc7b993b2011-06-16 04:13:47 +000027void f(Mat m, const Foo& const_foo, char *buffer) {
Nico Webere4a1c642011-06-14 16:14:58 +000028 S s;
29 S* ps = &s;
30 PS ps2 = &s;
31 char arr[5];
32 char* parr[5];
33 Foo foo;
Chandler Carruth000d4282011-06-16 09:09:40 +000034 char* heap_buffer = new char[42];
Nico Webere4a1c642011-06-14 16:14:58 +000035
36 /* Should warn */
37 memset(&s, 0, sizeof(&s)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000038 // 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)?}}
Nico Webere4a1c642011-06-14 16:14:58 +000039 memset(ps, 0, sizeof(ps)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000040 // 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)?}}
Nico Webere4a1c642011-06-14 16:14:58 +000041 memset(ps2, 0, sizeof(ps2)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000042 // 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)?}}
Nico Webere4a1c642011-06-14 16:14:58 +000043 memset(ps2, 0, sizeof(typeof(ps2))); // \
Chandler Carruth000d4282011-06-16 09:09:40 +000044 // expected-warning {{argument to 'sizeof' in 'memset' call is the same pointer type}}
Nico Webere4a1c642011-06-14 16:14:58 +000045 memset(ps2, 0, sizeof(PS)); // \
Chandler Carruth000d4282011-06-16 09:09:40 +000046 // expected-warning {{argument to 'sizeof' in 'memset' call is the same pointer type}}
47 memset(heap_buffer, 0, sizeof(heap_buffer)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000048 // 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?}}
Nico Webere4a1c642011-06-14 16:14:58 +000049
50 memcpy(&s, 0, sizeof(&s)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000051 // 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)?}}
Nico Webere4a1c642011-06-14 16:14:58 +000052 memcpy(0, &s, sizeof(&s)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000053 // 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)?}}
Nico Webere4a1c642011-06-14 16:14:58 +000054
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +000055 memmove(ps, 0, sizeof(ps)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000056 // 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)?}}
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +000057 memcmp(ps, 0, sizeof(ps)); // \
Anna Zaks90c78322012-05-30 23:14:52 +000058 // 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)?}}
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +000059
Nico Webere4a1c642011-06-14 16:14:58 +000060 /* 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));
Chandler Carruthc7b993b2011-06-16 04:13:47 +000079 char *cptr;
80 memcpy(&cptr, buffer, sizeof(cptr));
81 memcpy((char*)&cptr, buffer, sizeof(cptr));
Nico Webere4a1c642011-06-14 16:14:58 +000082
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
99 int* iparr[14];
100 memset(&iparr[0], 0, sizeof iparr);
101
102 memset(m, 0, sizeof(Mat));
103
104 // Copy to raw buffer shouldn't warn either
105 memcpy(&foo, &arr, sizeof(Foo));
106 memcpy(&arr, &foo, sizeof(Foo));
Peter Collingbournedca17612012-03-01 16:34:31 +0000107
108 // Shouldn't warn, and shouldn't crash either.
109 memset(({
110 if (0) {}
111 while (0) {}
112 for (;;) {}
113 &s;
114 }), 0, sizeof(s));
Nico Webere4a1c642011-06-14 16:14:58 +0000115}
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +0000116
117namespace ns {
118void memset(void* s, char c, int n);
119void f(int* i) {
120 memset(i, 0, sizeof(i));
121}
122}
Nico Webercda57822011-10-13 22:30:23 +0000123
124extern "C" int strncmp(const char *s1, const char *s2, unsigned n);
125extern "C" int strncasecmp(const char *s1, const char *s2, unsigned n);
126extern "C" char *strncpy(char *det, const char *src, unsigned n);
127extern "C" char *strncat(char *dst, const char *src, unsigned n);
128extern "C" char *strndup(const char *src, unsigned n);
129
130void strcpy_and_friends() {
131 const char* FOO = "<- should be an array instead";
132 const char* BAR = "<- this, too";
133
134 strncmp(FOO, BAR, sizeof(FOO)); // \
Anna Zaks90c78322012-05-30 23:14:52 +0000135 // 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?}}
Nico Webercda57822011-10-13 22:30:23 +0000136 strncasecmp(FOO, BAR, sizeof(FOO)); // \
Anna Zaks90c78322012-05-30 23:14:52 +0000137 // 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?}}
Nico Webercda57822011-10-13 22:30:23 +0000138
139 char buff[80];
140
141 strncpy(buff, BAR, sizeof(BAR)); // \
Anna Zaks90c78322012-05-30 23:14:52 +0000142 // 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?}}
Nico Webercda57822011-10-13 22:30:23 +0000143 strndup(FOO, sizeof(FOO)); // \
Anna Zaks90c78322012-05-30 23:14:52 +0000144 // 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?}}
Nico Webercda57822011-10-13 22:30:23 +0000145}