Debian Codegraph @

gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c

raw
1 /* Test -Wsizeof-pointer-memaccess warnings. */
2 /* { dg-do compile } */
3 /* { dg-options "-Wall -O2 -Wno-array-bounds -Wno-sizeof-array-argument -Wno-stringop-truncation -ftrack-macro-expansion=0" } */
4 /* { dg-options "-Wall -O2 -Wno-array-bounds -Wno-sizeof-array-argument -Wno-stringop-truncation -Wno-c++-compat -ftrack-macro-expansion=0" {target c} } */
5
6 #define bos(ptr) __builtin_object_size (ptr, 1)
7 #define bos0(ptr) __builtin_object_size (ptr, 0)
8
9 #define memset(dst, val, sz) __builtin___memset_chk (dst, val, sz, bos (dst))
10 #define memcpy(dst, src, sz) __builtin___memcpy_chk (dst, src, sz, bos (dst))
11 #define memmove(dst, src, sz) __builtin___memmove_chk (dst, src, sz, bos (dst))
12 #define strncpy(dst, src, sz) __builtin___strncpy_chk (dst, src, sz, bos (dst))
13 #define strncat(dst, src, sz) __builtin___strncat_chk (dst, src, sz, bos (dst))
14 #define stpncpy(dst, src, sz) __builtin___stpncpy_chk (dst, src, sz, bos (dst))
15
16 struct A { short a, b; int c, d; long e, f; };
17 typedef struct A TA;
18 typedef struct A *PA;
19 typedef TA *PTA;
20 struct B {};
21 typedef struct B TB;
22 typedef struct B *PB;
23 typedef TB *PTB;
24 typedef int X[3][3][3];
25
26 void
27 f1 (void *x)
28 {
29 struct A a, *pa1 = &a;
30 TA *pa2 = &a;
31 PA pa3 = &a;
32 PTA pa4 = &a;
33 memset (&a, 0, sizeof (&a)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
34 memset (pa1, 0, sizeof (pa1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
35 memset (pa2, 0, sizeof pa2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
36 memset (pa3, 0, sizeof (pa3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
37 memset (pa4, 0, sizeof pa4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
38 memset (pa1, 0, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
39 memset (pa2, 0, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
40 memset (pa3, 0, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
41 memset (pa4, 0, sizeof (__typeof (pa4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
42
43 memcpy (&a, x, sizeof (&a)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
44 memcpy (pa1, x, sizeof (pa1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
45 memcpy (pa2, x, sizeof pa2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
46 memcpy (pa3, x, sizeof (pa3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
47 memcpy (pa4, x, sizeof pa4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
48 memcpy (pa1, x, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
49 memcpy (pa2, x, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
50 memcpy (pa3, x, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
51 memcpy (pa4, x, sizeof (__typeof (pa4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
52
53 memcpy (x, &a, sizeof (&a)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
54 memcpy (x, pa1, sizeof (pa1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
55 memcpy (x, pa2, sizeof pa2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
56 memcpy (x, pa3, sizeof (pa3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
57 memcpy (x, pa4, sizeof pa4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
58 memcpy (x, pa1, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
59 memcpy (x, pa2, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
60 memcpy (x, pa3, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
61 memcpy (x, pa4, sizeof (__typeof (pa4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
62
63 memmove (&a, x, sizeof (&a)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
64 memmove (pa1, x, sizeof (pa1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
65 memmove (pa2, x, sizeof pa2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
66 memmove (pa3, x, sizeof (pa3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
67 memmove (pa4, x, sizeof pa4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
68 memmove (pa1, x, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
69 memmove (pa2, x, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
70 memmove (pa3, x, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
71 memmove (pa4, x, sizeof (__typeof (pa4)));/* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
72
73 memmove (x, &a, sizeof (&a)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
74 memmove (x, pa1, sizeof (pa1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
75 memmove (x, pa2, sizeof pa2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
76 memmove (x, pa3, sizeof (pa3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
77 memmove (x, pa4, sizeof pa4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
78 memmove (x, pa1, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
79 memmove (x, pa2, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
80 memmove (x, pa3, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
81 memmove (x, pa4, sizeof (__typeof (pa4)));/* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
82
83 /* These are correct, no warning. */
84 memset (&a, 0, sizeof a);
85 memset (&a, 0, sizeof (a));
86 memset (&a, 0, sizeof (struct A));
87 memset (&a, 0, sizeof (const struct A));
88 memset (&a, 0, sizeof (volatile struct A));
89 memset (&a, 0, sizeof (volatile const struct A));
90 memset (&a, 0, sizeof (TA));
91 memset (&a, 0, sizeof (__typeof (*&a)));
92 memset (pa1, 0, sizeof (*pa1));
93 memset (pa2, 0, sizeof (*pa3));
94 memset (pa3, 0, sizeof (__typeof (*pa3)));
95 /* These are probably broken, but obfuscated, no warning. */
96 memset ((void *) &a, 0, sizeof (&a));
97 memset ((char *) &a, 0, sizeof (&a));
98 memset (&a, 0, sizeof (&a) + 0);
99 memset (&a, 0, 0 + sizeof (&a));
100
101 /* These are correct, no warning. */
102 memcpy (&a, x, sizeof a);
103 memcpy (&a, x, sizeof (a));
104 memcpy (&a, x, sizeof (struct A));
105 memcpy (&a, x, sizeof (const struct A));
106 memcpy (&a, x, sizeof (volatile struct A));
107 memcpy (&a, x, sizeof (volatile const struct A));
108 memcpy (&a, x, sizeof (TA));
109 memcpy (&a, x, sizeof (__typeof (*&a)));
110 memcpy (pa1, x, sizeof (*pa1));
111 memcpy (pa2, x, sizeof (*pa3));
112 memcpy (pa3, x, sizeof (__typeof (*pa3)));
113 /* These are probably broken, but obfuscated, no warning. */
114 memcpy ((void *) &a, x, sizeof (&a));
115 memcpy ((char *) &a, x, sizeof (&a));
116 memcpy (&a, x, sizeof (&a) + 0);
117 memcpy (&a, x, 0 + sizeof (&a));
118
119 /* These are correct, no warning. */
120 memcpy (x, &a, sizeof a);
121 memcpy (x, &a, sizeof (a));
122 memcpy (x, &a, sizeof (struct A));
123 memcpy (x, &a, sizeof (const struct A));
124 memcpy (x, &a, sizeof (volatile struct A));
125 memcpy (x, &a, sizeof (volatile const struct A));
126 memcpy (x, &a, sizeof (TA));
127 memcpy (x, &a, sizeof (__typeof (*&a)));
128 memcpy (x, pa1, sizeof (*pa1));
129 memcpy (x, pa2, sizeof (*pa3));
130 memcpy (x, pa3, sizeof (__typeof (*pa3)));
131 /* These are probably broken, but obfuscated, no warning. */
132 memcpy (x, (void *) &a, sizeof (&a));
133 memcpy (x, (char *) &a, sizeof (&a));
134 memcpy (x, &a, sizeof (&a) + 0);
135 memcpy (x, &a, 0 + sizeof (&a));
136
137 /* These are correct, no warning. */
138 memmove (&a, x, sizeof a);
139 memmove (&a, x, sizeof (a));
140 memmove (&a, x, sizeof (struct A));
141 memmove (&a, x, sizeof (const struct A));
142 memmove (&a, x, sizeof (volatile struct A));
143 memmove (&a, x, sizeof (volatile const struct A));
144 memmove (&a, x, sizeof (TA));
145 memmove (&a, x, sizeof (__typeof (*&a)));
146 memmove (pa1, x, sizeof (*pa1));
147 memmove (pa2, x, sizeof (*pa3));
148 memmove (pa3, x, sizeof (__typeof (*pa3)));
149 /* These are probably broken, but obfuscated, no warning. */
150 memmove ((void *) &a, x, sizeof (&a));
151 memmove ((char *) &a, x, sizeof (&a));
152 memmove (&a, x, sizeof (&a) + 0);
153 memmove (&a, x, 0 + sizeof (&a));
154
155 /* These are correct, no warning. */
156 memmove (x, &a, sizeof a);
157 memmove (x, &a, sizeof (a));
158 memmove (x, &a, sizeof (struct A));
159 memmove (x, &a, sizeof (const struct A));
160 memmove (x, &a, sizeof (volatile struct A));
161 memmove (x, &a, sizeof (volatile const struct A));
162 memmove (x, &a, sizeof (TA));
163 memmove (x, &a, sizeof (__typeof (*&a)));
164 memmove (x, pa1, sizeof (*pa1));
165 memmove (x, pa2, sizeof (*pa3));
166 memmove (x, pa3, sizeof (__typeof (*pa3)));
167 /* These are probably broken, but obfuscated, no warning. */
168 memmove (x, (void *) &a, sizeof (&a));
169 memmove (x, (char *) &a, sizeof (&a));
170 memmove (x, &a, sizeof (&a) + 0);
171 memmove (x, &a, 0 + sizeof (&a));
172 }
173
174 void
175 f2 (void *x)
176 {
177 struct B b, *pb1 = &b;
178 TB *pb2 = &b;
179 PB pb3 = &b;
180 PTB pb4 = &b;
181 memset (&b, 0, sizeof (&b)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
182 memset (pb1, 0, sizeof (pb1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
183 memset (pb2, 0, sizeof pb2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
184 memset (pb3, 0, sizeof (pb3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
185 memset (pb4, 0, sizeof pb4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
186 memset (pb1, 0, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
187 memset (pb2, 0, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
188 memset (pb3, 0, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
189 memset (pb4, 0, sizeof (__typeof (pb4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
190
191 memcpy (&b, x, sizeof (&b)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
192 memcpy (pb1, x, sizeof (pb1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
193 memcpy (pb2, x, sizeof pb2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
194 memcpy (pb3, x, sizeof (pb3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
195 memcpy (pb4, x, sizeof pb4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
196 memcpy (pb1, x, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
197 memcpy (pb2, x, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
198 memcpy (pb3, x, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
199 memcpy (pb4, x, sizeof (__typeof (pb4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
200
201 memcpy (x, &b, sizeof (&b)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
202 memcpy (x, pb1, sizeof (pb1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
203 memcpy (x, pb2, sizeof pb2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
204 memcpy (x, pb3, sizeof (pb3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
205 memcpy (x, pb4, sizeof pb4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
206 memcpy (x, pb1, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
207 memcpy (x, pb2, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
208 memcpy (x, pb3, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
209 memcpy (x, pb4, sizeof (__typeof (pb4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
210
211 memmove (&b, x, sizeof (&b)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
212 memmove (pb1, x, sizeof (pb1)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
213 memmove (pb2, x, sizeof pb2); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
214 memmove (pb3, x, sizeof (pb3)); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
215 memmove (pb4, x, sizeof pb4); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
216 memmove (pb1, x, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
217 memmove (pb2, x, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
218 memmove (pb3, x, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
219 memmove (pb4, x, sizeof (__typeof (pb4)));/* { dg-warning "call is the same pointer type \[^\n\r\]* as the destination; expected \[^\n\r\]* or an explicit length" } */
220
221 memmove (x, &b, sizeof (&b)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
222 memmove (x, pb1, sizeof (pb1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
223 memmove (x, pb2, sizeof pb2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
224 memmove (x, pb3, sizeof (pb3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
225 memmove (x, pb4, sizeof pb4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
226 memmove (x, pb1, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
227 memmove (x, pb2, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
228 memmove (x, pb3, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
229 memmove (x, pb4, sizeof (__typeof (pb4)));/* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
230
231 /* These are correct, no warning. */
232 memset (&b, 0, sizeof b);
233 memset (&b, 0, sizeof (b));
234 memset (&b, 0, sizeof (struct B));
235 memset (&b, 0, sizeof (const struct B));
236 memset (&b, 0, sizeof (volatile struct B));
237 memset (&b, 0, sizeof (volatile const struct B));
238 memset (&b, 0, sizeof (TB));
239 memset (&b, 0, sizeof (__typeof (*&b)));
240 memset (pb1, 0, sizeof (*pb1));
241 memset (pb2, 0, sizeof (*pb3));
242 memset (pb3, 0, sizeof (__typeof (*pb3)));
243 /* These are probably broken, but obfuscated, no warning. */
244 memset ((void *) &b, 0, sizeof (&b));
245 memset ((char *) &b, 0, sizeof (&b));
246 memset (&b, 0, sizeof (&b) + 0);
247 memset (&b, 0, 0 + sizeof (&b));
248
249 /* These are correct, no warning. */
250 memcpy (&b, x, sizeof b);
251 memcpy (&b, x, sizeof (b));
252 memcpy (&b, x, sizeof (struct B));
253 memcpy (&b, x, sizeof (const struct B));
254 memcpy (&b, x, sizeof (volatile struct B));
255 memcpy (&b, x, sizeof (volatile const struct B));
256 memcpy (&b, x, sizeof (TB));
257 memcpy (&b, x, sizeof (__typeof (*&b)));
258 memcpy (pb1, x, sizeof (*pb1));
259 memcpy (pb2, x, sizeof (*pb3));
260 memcpy (pb3, x, sizeof (__typeof (*pb3)));
261 /* These are probably broken, but obfuscated, no warning. */
262 memcpy ((void *) &b, x, sizeof (&b));
263 memcpy ((char *) &b, x, sizeof (&b));
264 memcpy (&b, x, sizeof (&b) + 0);
265 memcpy (&b, x, 0 + sizeof (&b));
266
267 /* These are correct, no warning. */
268 memcpy (x, &b, sizeof b);
269 memcpy (x, &b, sizeof (b));
270 memcpy (x, &b, sizeof (struct B));
271 memcpy (x, &b, sizeof (const struct B));
272 memcpy (x, &b, sizeof (volatile struct B));
273 memcpy (x, &b, sizeof (volatile const struct B));
274 memcpy (x, &b, sizeof (TB));
275 memcpy (x, &b, sizeof (__typeof (*&b)));
276 memcpy (x, pb1, sizeof (*pb1));
277 memcpy (x, pb2, sizeof (*pb3));
278 memcpy (x, pb3, sizeof (__typeof (*pb3)));
279 /* These are probably broken, but obfuscated, no warning. */
280 memcpy (x, (void *) &b, sizeof (&b));
281 memcpy (x, (char *) &b, sizeof (&b));
282 memcpy (x, &b, sizeof (&b) + 0);
283 memcpy (x, &b, 0 + sizeof (&b));
284
285 /* These are correct, no warning. */
286 memmove (&b, x, sizeof b);
287 memmove (&b, x, sizeof (b));
288 memmove (&b, x, sizeof (struct B));
289 memmove (&b, x, sizeof (const struct B));
290 memmove (&b, x, sizeof (volatile struct B));
291 memmove (&b, x, sizeof (volatile const struct B));
292 memmove (&b, x, sizeof (TB));
293 memmove (&b, x, sizeof (__typeof (*&b)));
294 memmove (pb1, x, sizeof (*pb1));
295 memmove (pb2, x, sizeof (*pb3));
296 memmove (pb3, x, sizeof (__typeof (*pb3)));
297 /* These are probably broken, but obfuscated, no warning. */
298 memmove ((void *) &b, x, sizeof (&b));
299 memmove ((char *) &b, x, sizeof (&b));
300 memmove (&b, x, sizeof (&b) + 0);
301 memmove (&b, x, 0 + sizeof (&b));
302
303 /* These are correct, no warning. */
304 memmove (x, &b, sizeof b);
305 memmove (x, &b, sizeof (b));
306 memmove (x, &b, sizeof (struct B));
307 memmove (x, &b, sizeof (const struct B));
308 memmove (x, &b, sizeof (volatile struct B));
309 memmove (x, &b, sizeof (volatile const struct B));
310 memmove (x, &b, sizeof (TB));
311 memmove (x, &b, sizeof (__typeof (*&b)));
312 memmove (x, pb1, sizeof (*pb1));
313 memmove (x, pb2, sizeof (*pb3));
314 memmove (x, pb3, sizeof (__typeof (*pb3)));
315 /* These are probably broken, but obfuscated, no warning. */
316 memmove (x, (void *) &b, sizeof (&b));
317 memmove (x, (char *) &b, sizeof (&b));
318 memmove (x, &b, sizeof (&b) + 0);
319 memmove (x, &b, 0 + sizeof (&b));
320 }
321
322 void
323 f3 (void *x, char *y, int z, X w)
324 {
325 unsigned char *y1 = (unsigned char *) __builtin_alloca (z + 16);
326 char buf1[7];
327 signed char buf2[z + 32];
328 long buf3[17];
329 int *buf4[9];
330 signed char *y2 = buf2;
331 char c;
332 char *y3;
333 memset (y, 0, sizeof (y)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
334 memset (y1, 0, sizeof (y1)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
335 memset (y2, 0, sizeof (y2)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
336 memset (&c, 0, sizeof (&c)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
337 memset (w, 0, sizeof w); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
338
339 memcpy (y, x, sizeof (y)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
340 memcpy (y1, x, sizeof (y1)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
341 memcpy (y2, x, sizeof (y2)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
342 memcpy (&c, x, sizeof (&c)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
343 memcpy (w, x, sizeof w); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
344
345 memcpy (x, y, sizeof (y)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
346 memcpy (x, y1, sizeof (y1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
347 memcpy (x, y2, sizeof (y2)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
348 memcpy (x, &c, sizeof (&c)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
349 memcpy (x, w, sizeof w); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
350
351 memmove (y, x, sizeof (y)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
352 memmove (y1, x, sizeof (y1)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
353 memmove (y2, x, sizeof (y2)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
354 memmove (&c, x, sizeof (&c)); /* { dg-warning "call is the same expression as the destination; did you mean to remove the addressof" } */
355 memmove (w, x, sizeof w); /* { dg-warning "call is the same expression as the destination; did you mean to dereference it" } */
356
357 memmove (x, y, sizeof (y)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
358 memmove (x, y1, sizeof (y1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
359 memmove (x, y2, sizeof (y2)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
360 memmove (x, &c, sizeof (&c)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
361 memmove (x, w, sizeof w); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
362
363 /* These are correct, no warning. */
364 memset (y, 0, sizeof (*y));
365 memset (y1, 0, sizeof (*y2));
366 memset (buf1, 0, sizeof buf1);
367 memset (buf3, 0, sizeof (buf3));
368 memset (&buf3[0], 0, sizeof (buf3));
369 memset (&buf4[0], 0, sizeof (buf4));
370 memset (w, 0, sizeof (X));
371 /* These are probably broken, but obfuscated, no warning. */
372 memset ((void *) y, 0, sizeof (y));
373 memset ((char *) y1, 0, sizeof (y2));
374 memset (y, 0, sizeof (y) + 0);
375 memset (y1, 0, 0 + sizeof (y2));
376 memset ((void *) &c, 0, sizeof (&c));
377 memset ((signed char *) &c, 0, sizeof (&c));
378 memset (&c, 0, sizeof (&c) + 0);
379 memset (&c, 0, 0 + sizeof (&c));
380
381 /* These are correct, no warning. */
382 memcpy (y, x, sizeof (*y));
383 memcpy (y1, x, sizeof (*y2));
384 memcpy (buf1, x, sizeof buf1);
385 memcpy (buf3, x, sizeof (buf3));
386 memcpy (&buf3[0], x, sizeof (buf3));
387 memcpy (&buf4[0], x, sizeof (buf4));
388 memcpy (&y3, y, sizeof (y3));
389 memcpy ((char *) &y3, y, sizeof (y3));
390 memcpy (w, x, sizeof (X));
391 /* These are probably broken, but obfuscated, no warning. */
392 memcpy ((void *) y, x, sizeof (y));
393 memcpy ((char *) y1, x, sizeof (y2));
394 memcpy (y, x, sizeof (y) + 0);
395 memcpy (y1, x, 0 + sizeof (y2));
396 memcpy ((void *) &c, x, sizeof (&c));
397 memcpy ((signed char *) &c, x, sizeof (&c));
398 memcpy (&c, x, sizeof (&c) + 0);
399 memcpy (&c, x, 0 + sizeof (&c));
400
401 /* These are correct, no warning. */
402 memcpy (x, y, sizeof (*y));
403 memcpy (x, y1, sizeof (*y2));
404 memcpy (x, buf1, sizeof buf1);
405 memcpy (x, buf3, sizeof (buf3));
406 memcpy (x, &buf3[0], sizeof (buf3));
407 memcpy (x, &buf4[0], sizeof (buf4));
408 memcpy (y, &y3, sizeof (y3));
409 memcpy (y, (char *) &y3, sizeof (y3));
410 memcpy (x, w, sizeof (X));
411 /* These are probably broken, but obfuscated, no warning. */
412 memcpy (x, (void *) y, sizeof (y));
413 memcpy (x, (char *) y1, sizeof (y2));
414 memcpy (x, y, sizeof (y) + 0);
415 memcpy (x, y1, 0 + sizeof (y2));
416 memcpy (x, (void *) &c, sizeof (&c));
417 memcpy (x, (signed char *) &c, sizeof (&c));
418 memcpy (x, &c, sizeof (&c) + 0);
419 memcpy (x, &c, 0 + sizeof (&c));
420
421 /* These are correct, no warning. */
422 memmove (y, x, sizeof (*y));
423 memmove (y1, x, sizeof (*y2));
424 memmove (buf1, x, sizeof buf1);
425 memmove (buf3, x, sizeof (buf3));
426 memmove (&buf3[0], x, sizeof (buf3));
427 memmove (&buf4[0], x, sizeof (buf4));
428 memmove (&y3, y, sizeof (y3));
429 memmove ((char *) &y3, y, sizeof (y3));
430 memmove (w, x, sizeof (X));
431 /* These are probably broken, but obfuscated, no warning. */
432 memmove ((void *) y, x, sizeof (y));
433 memmove ((char *) y1, x, sizeof (y2));
434 memmove (y, x, sizeof (y) + 0);
435 memmove (y1, x, 0 + sizeof (y2));
436 memmove ((void *) &c, x, sizeof (&c));
437 memmove ((signed char *) &c, x, sizeof (&c));
438 memmove (&c, x, sizeof (&c) + 0);
439 memmove (&c, x, 0 + sizeof (&c));
440
441 /* These are correct, no warning. */
442 memmove (x, y, sizeof (*y));
443 memmove (x, y1, sizeof (*y2));
444 memmove (x, buf1, sizeof buf1);
445 memmove (x, buf3, sizeof (buf3));
446 memmove (x, &buf3[0], sizeof (buf3));
447 memmove (x, &buf4[0], sizeof (buf4));
448 memmove (y, &y3, sizeof (y3));
449 memmove (y, (char *) &y3, sizeof (y3));
450 memmove (x, w, sizeof (X));
451 /* These are probably broken, but obfuscated, no warning. */
452 memmove (x, (void *) y, sizeof (y));
453 memmove (x, (char *) y1, sizeof (y2));
454 memmove (x, y, sizeof (y) + 0);
455 memmove (x, y1, 0 + sizeof (y2));
456 memmove (x, (void *) &c, sizeof (&c));
457 memmove (x, (signed char *) &c, sizeof (&c));
458 memmove (x, &c, sizeof (&c) + 0);
459 memmove (x, &c, 0 + sizeof (&c));
460 }
461
462 void
463 f4 (char *x, char **y, int z, char w[64])
464 {
465 const char *s1 = "foobarbaz";
466 const char *s2 = "abcde12345678";
467 strncpy (x, s1, sizeof (s1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
468 strncat (x, s2, sizeof (s2)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
469 stpncpy (x, s1, sizeof (s1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
470
471 strncpy (w, s1, sizeof (w)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
472 strncat (w, s2, sizeof (w)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
473 stpncpy (w, s1, sizeof (w)); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
474
475 /* These are pointless when the destination is large enough, and
476 cause overflow otherwise. If the copies are guaranteed to be
477 safe the calls might as well be replaced by strcat(), strcpy(),
478 or memcpy(). */
479 const char s3[] = "foobarbaz";
480 const char s4[] = "abcde12345678";
481 strncpy (x, s3, sizeof (s3)); /* { dg-warning "call is the same expression as the source; did you mean to use the size of the destination?" } */
482 strncat (x, s4, sizeof (s4)); /* { dg-warning "call is the same expression as the source; did you mean to use the size of the destination?" } */
483 stpncpy (x, s3, sizeof (s3)); /* { dg-warning "call is the same expression as the source; did you mean to use the size of the destination?" } */
484 }
485
486 /* { dg-prune-output "\[\n\r\]*writing\[\n\r\]*" } */
487 /* { dg-prune-output "\[\n\r\]*reading\[\n\r\]*" } */