1 /* PR 35503 - Warn about restricted pointers
3 { dg-options "-O2 -Wrestrict -ftrack-macro-expansion=0" } */
5 #include "../gcc.dg/range.h"
12 # define restrict __restrict
16 extern void* memcpy (void* restrict
, const void* restrict
, size_t);
17 extern void* mempcpy (void* restrict
, const void* restrict
, size_t);
18 extern void* memmove (void*, const void*, size_t);
20 extern char* stpcpy (char* restrict
, const char* restrict
);
22 extern char* strcat (char* restrict
, const char* restrict
);
23 extern char* strcpy (char* restrict
, const char* restrict
);
24 extern char* strncpy (char* restrict
, const char* restrict
, size_t);
30 void sink (void*, ...);
39 /* Exercise memcpy with constant or known arguments. */
41 void test_memcpy_cst (void *d
, const void *s
)
44 #define T(dst, src, n) do { \
45 if (!LINE || LINE == __LINE__) { \
46 char a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; \
48 const void *ps = (src); \
49 memcpy (pd, ps, (n)); \
55 T (a
, s
= a
, 3); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
57 /* This isn't detected because memcpy calls with small power-of-2 sizes
58 are intentionally folded into safe copies equivalent to memmove.
59 It's marked xfail only because there is value in detecting such
60 invalid calls for portability, and as a reminder of why it isn't
62 T (a
, a
+ 1, 1); /* { dg-warning "\\\[-Wrestrict" "memcpy with a small power of 2 size" { xfail *-*-* } } */
64 T (a
, a
+ 3, 5); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
67 char a
[3] = { 1, 2, 3 };
69 /* Verify that a call to memcpy with an exact overlap is diagnosed
70 (also tested above) but an excplicit one to __builtin_memcpy is
71 not. See bug 32667 for the rationale. */
72 (memcpy
)(a
, a
, sizeof a
); /* { dg-warning "source argument is the same as destination" "memcpy" } */
75 __builtin_memcpy (a
, a
, sizeof a
);
85 memcpy (d
, s
, sizeof a
[0]);
90 /* The following is only diagnosed for sizes that aren't small
92 memcpy (d
, s
, sizeof a
[0] + 2); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
97 memcpy (d
, s
, sizeof a
[0]);
102 memcpy (d
, s
, sizeof a
[0] + 2); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
116 memcpy (d
, s
, sizeof x
.a
);
121 memcpy (d
, s
, sizeof x
.a
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
126 memcpy (d
, s
, sizeof x
.a
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
136 memcpy (d
, s
, 3); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
151 memcpy (d
, s
, sizeof x
.b
);
156 memcpy (d
, s
, sizeof x
.b
);
161 memcpy (d
, s
, sizeof x
.b
);
166 memcpy (d
, s
, sizeof x
.b
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
172 #define T(dst, src, n) do { \
173 if (!LINE || LINE == __LINE__) { \
174 char a[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; \
175 memcpy ((dst), (src), (n)); \
180 /* Verify the offset of the overlap is the same regardless of whether
181 the destination is at lower or higher offset than the source. */
182 T (a
, a
+ 1, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 1 overlaps 4 bytes at offset 1" "memcpy" } */
183 T (a
, a
+ 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "memcpy" } */
184 T (a
, a
+ 3, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 3 overlaps 2 bytes at offset 3" "memcpy" } */
186 T (a
+ 1, a
, 5); /* { dg-warning "accessing 5 bytes at offsets 1 and 0 overlaps 4 bytes at offset 1" "memcpy" } */
187 T (a
+ 2, a
, 5); /* { dg-warning "accessing 5 bytes at offsets 2 and 0 overlaps 3 bytes at offset 2" "memcpy" } */
188 T (a
+ 3, a
, 5); /* { dg-warning "accessing 5 bytes at offsets 3 and 0 overlaps 2 bytes at offset 3" "memcpy" } */
192 /* Exercise memcpy with destination or source offset or size in
193 a determinate range. */
195 void test_memcpy_range (char *d
, size_t sz
)
198 #define T(dst, src, n) do { \
199 if (!LINE || LINE == __LINE__) { \
200 char a[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; \
202 const void *ps = (src); \
203 memcpy (pd, ps, (n)); \
208 ptrdiff_t ir
= SR (2, 3);
213 /* The following fails because the size is a small power of 2. */
214 T (a
+ ir
, a
, 4); /* { dg-warning "accessing 4 bytes at offsets \\\[2, 3] and 0 overlaps between 1 and 2 bytes at offset \\\[3, 2]" "memcpy" { xfail *-*-*} } */
215 T (a
+ ir
, a
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 3] and 0 overlaps between 2 and 3 bytes at offset \\\[2, 3]" "memcpy" } */
221 T (d
+ ir
, d
, 4); /* { dg-warning "accessing 4 bytes at offsets \\\[2, 3] and 0 overlaps 1 byte at offset 3" "bug 79220" { xfail *-*-* } } */
222 T (d
+ ir
, d
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 3] and 0 overlaps between 2 and 3 bytes at offset \\\[2, 3]" "memcpy" } */
224 /* Because the size is constant and a power of 2 the following is
225 folded too early to detect the overlap. */
226 T (d
+ ir
, d
, 4); /* { dg-warning "accessing 4 bytes at offsets \\\[2, 3] and 0 overlaps 2 byte at offset 2" "" { xfail *-*-* } } */
227 T (d
+ ir
, d
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 3] and 0 overlaps between 2 and 3 bytes at offset \\\[2, 3]" "memcpy" } */
229 /* Exercise the full range of size_t. */
234 T (a
, a
+ 1, SR (0, 1));
235 T (a
, a
+ 1, SR (0, 2));
236 T (a
, a
+ 1, SR (1, 2));
237 T (a
, a
+ 1, SR (2, 3)); /* { dg-warning "accessing between 2 and 3 bytes at offsets 0 and 1 overlaps between 1 and 2 bytes at offset 1" "memcpy" } */
238 T (a
, a
+ 1, UR (2, DIFF_MAX
+ (size_t)1)); /* { dg-warning "accessing 2 or more bytes at offsets 0 and 1 overlaps 1 or more bytes at offset 1" "memcpy" } */
239 T (a
, a
+ 1, UR (2, SIZE_MAX
- 1)); /* { dg-warning "accessing 2 or more bytes at offsets 0 and 1 overlaps 1 or more bytes at offset 1" "memcpy" } */
240 T (a
, a
+ 2, SR (2, 3));
241 T (a
, a
+ 2, SR (3, 4)); /* { dg-warning "accessing between 3 and 4 bytes at offsets 0 and 2 overlaps between 1 and 2 bytes at offset 2" "memcpy" } */
242 T (a
, a
+ 3, SR (3, 4));
243 T (a
, a
+ 3, SR (4, 5)); /* { dg-warning "accessing between 4 and 5 bytes at offsets 0 and 3 overlaps between 1 and 2 bytes at offset 3" "memcpy" } */
244 T (a
, a
+ 3, SR (5, 6)); /* { dg-warning "accessing between 5 and 6 bytes at offsets 0 and 3 overlaps between 2 and 3 bytes at offset 3" "memcpy" } */
246 T (a
+ 1, a
, SR (0, 1));
247 T (a
+ 1, a
, SR (0, 2));
248 T (a
+ 1, a
, SR (1, 2));
249 T (a
+ 1, a
, SR (2, 3)); /* { dg-warning "accessing between 2 and 3 bytes at offsets 1 and 0 overlaps between 1 and 2 bytes at offset 1" "memcpy" } */
250 T (a
+ 1, a
, UR (2, DIFF_MAX
+ (size_t)1)); /* { dg-warning "accessing 2 or more bytes at offsets 1 and 0 overlaps 1 or more bytes at offset 1" "memcpy" } */
251 T (a
+ 1, a
, UR (2, SIZE_MAX
- 1)); /* { dg-warning "accessing 2 or more bytes at offsets 1 and 0 overlaps 1 or more bytes at offset 1" "memcpy" } */
252 T (a
+ 2, a
, SR (2, 3));
253 T (a
+ 2, a
, SR (3, 4)); /* { dg-warning "accessing between 3 and 4 bytes at offsets 2 and 0 overlaps between 1 and 2 bytes at offset 2" "memcpy" } */
254 T (a
+ 3, a
, SR (3, 4));
255 T (a
+ 3, a
, SR (4, 5)); /* { dg-warning "accessing between 4 and 5 bytes at offsets 3 and 0 overlaps between 1 and 2 bytes at offset 3" "memcpy" } */
256 T (a
+ 3, a
, SR (5, 6)); /* { dg-warning "accessing between 5 and 6 bytes at offsets 3 and 0 overlaps between 2 and 3 bytes at offset 3" "memcpy" } */
260 T (a
, a
+ ir
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[2, 5] overlaps between 1 and 3 bytes at offset \\\[2, 4]" "memcpy" } */
261 T (a
, a
+ ir
, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and \\\[2, 5] overlaps between 3 and 4 bytes at offset \\\[2, 3]" "memcpy" } */
263 /* Below, there are two possible regions for the source of the copy:
264 1) one just before the high end of the address space that's 3
265 bytes large close to the lower end of the offset range, and
266 2) another in the [DIFF_MIN, -8] range from D and so at least
268 A copy from (1) overlaps but one from (2) does not. Verify that
269 the copy is not diagnosed. (This test case was reduced from
270 the Linux kernel.) */
271 T (d
, d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), 5);
272 T (d
, d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), 6);
273 T (d
, d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), 7);
274 T (d
, d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), 9);
276 T (d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), d
, 5);
277 T (d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), d
, 6);
278 T (d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), d
, 7);
279 T (d
+ UR (DIFF_MAX
- 3, SIZE_MAX
- 7), d
, 9);
282 /* Create an offset in the range [0, -1]. */
288 /* Exercise memcpy with both destination and source pointer offsets
289 in some known range. */
290 size_t n
= UR (3, 4);
291 T (a
+ SR (1, 3), a
+ SR (1, 3), n
); /* { dg-warning "accessing between 3 and 4 bytes at offsets \\\[1, 3] and \\\[1, 3] overlaps between 1 and 4 bytes at offset \\\[1, 3]" "memcpy" } */
293 /* This is an interesting case:
294 memcpy (a + i, a + j, n) with i in [1, 3], j in [2, 3], and n in [3, 4]
295 we have the following possibilities ('^' denotesthe destination offset,
296 '*' marks the overlap, and '?' is the possible overlap for large n):
297 i j | a = 012345678 SIZ OFF (size and offset of the overlap)
303 There are two ways to present the results:
304 1) overlaps between 1 and 4 bytes at offset [2, 3]
305 2) overlaps between 1 and 4 bytes at offset 2. */
306 T (a
+ SR (1, 3), a
+ SR (2, 3), n
); /* { dg-warning "accessing between 3 and 4 bytes at offsets \\\[1, 3] and \\\[2, 3] overlaps between 1 and 4 bytes at offset \\\[2, 3]" "memcpy" } */
307 T (a
+ SR (1, 3), a
+ SR (3, 4), n
);
309 T (a
+ SR (2, 3), a
+ SR (3, 4), n
); /* { dg-warning "accessing between 3 and 4 bytes at offsets \\\[2, 3] and \\\[3, 4] overlaps between 1 and 4 bytes at offset \\\[3, 4]" "memcpy" } */
311 T (a
+ SR (1, 3), a
+ SR (4, 5), n
);
312 T (a
+ SR (2, 3), a
+ SR (4, 5), n
);
313 T (a
+ SR (3, 4), a
+ SR (4, 5), n
); /* { dg-warning "accessing between 3 and 4 bytes at offsets \\\[3, 4] and \\\[4, 5] overlaps between 1 and 4 bytes at offset \\\[4, 5]" "memcpy" } */
315 /* Exercise the full range of size_t. */
321 /* Exercise memcpy with offset and/or size in a determinate anti-range. */
323 void test_memcpy_anti_range (char *d
, const char *s
)
325 T (d
, d
+ SAR (0, 3), 1);
326 T (d
, d
+ SAR (0, 3), 2);
327 T (d
, d
+ SAR (0, 3), 3);
328 T (d
, d
+ SAR (0, 3), DIFF_MAX
- 2); /* { dg-warning "overlaps \[0-9\]+ bytes at offset 2" } */
329 T (d
, d
+ SAR (0, 3), DIFF_MAX
- 1); /* { dg-warning "overlaps \[0-9\]+ bytes at offset 1" } */
330 T (d
, d
+ SAR (0, 3), DIFF_MAX
); /* { dg-warning "overlaps \[0-9\]+ bytes at offset 0" } */
332 T (d
, d
+ SAR (0, 3), UR (DIFF_MAX
- 2, DIFF_MAX
)); /* { dg-warning "accessing \[0-9\]+ or more bytes at offsets 0 and \\\[-?\[0-9\]+, -?\[0-9\]+] overlaps \[0-9\]+ bytes at offset 2" } */
334 /* Verify that a size in an anti-range ~[0, N] where N >= PTRDIFF_MAX
335 doesn't trigger a warning. */
336 T (d
, s
, UAR (1, DIFF_MAX
- 1));
337 T (d
, s
, UAR (1, DIFF_MAX
));
338 T (d
, s
, UAR (1, SIZE_MAX
- 1));
340 /* This causes the last dg-warning test to fail for some reason.
341 T (d, s, UAR (1, SIZE_MAX)); */
344 /* Verify calls to memcpy() where the combination of offsets in some
345 range and size is such that either overlap is unavoidable or one
346 or both offsets would exceed the maximum size of an object
349 void test_memcpy_range_exceed (char *d
, const char *s
)
351 /* Verify offset and size both in some range. The memcpy checking
352 is less strict than that of string functions like strncpy and
353 doesn't trigger unless the overlap is certain. The following
354 overlaps for (r == 3 && n > 3) but not, for example, for
355 (r == 4 && n == 4), and so it's not diagnosed. */
356 ptrdiff_t i
= SR (3, 5);
357 size_t n
= UR (4, 6);
361 /* Ditto for objects of unknown sizes. */
365 /* Verify that a warning is issued for a copy between two regions
366 whose aggregate size would exceed DIFF_MAX if it were to not
368 T (d
, s
, DIFF_MAX
/ 2);
369 T (d
, s
, DIFF_MAX
/ 2 + 1); /* { dg-warning "overlaps 1 byte" "memcpy" } */
370 T (d
, s
, DIFF_MAX
/ 2 + 2); /* { dg-warning "overlaps 3 bytes" "memcpy" } */
371 T (d
, s
, DIFF_MAX
/ 2 + 3); /* { dg-warning "overlaps 5 bytes" "memcpy" } */
373 i
= SR (DIFF_MAX
- 2, DIFF_MAX
);
375 /* Verify a warning for an out-of-bounds offset range and constant
377 T (d
, d
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 byte" "memcpy" } */
378 T (d
+ i
, d
, 3); /* { dg-warning "accessing 3 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 0 overlaps 1 byte" "memcpy" } */
380 T (d
+ 1, d
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 1 and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 byte" "memcpy" } */
381 T (d
+ i
, d
+ 1, 3); /* { dg-warning "accessing 3 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 1 overlaps 1 byte" "memcpy" } */
383 /* Verify that the warnings above are independent of whether the source
384 and destination are known to be based on the same object. */
385 T (d
, s
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 byte" "memcpy" } */
386 T (d
+ i
, s
, 3); /* { dg-warning "accessing 3 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 0 overlaps 1 byte" "memcpy" } */
388 T (d
+ 1, s
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 1 and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 byte" "memcpy" } */
389 T (d
+ i
, s
+ 1, 3); /* { dg-warning "accessing 3 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 1 overlaps 1 byte" "memcpy" } */
391 #if __SIZEOF_SIZE_T__ == 8
392 /* Verfiy the offset and size computation is correct. The overlap
393 offset mentioned in the warning plus sthe size of the access must
394 not exceed DIFF_MAX. */
395 T (d
, d
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[9223372036854775805, 9223372036854775807] overlaps 3 bytes at offset 9223372036854775802" "LP64" { target lp64 } } */
396 T (d
+ i
, d
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[9223372036854775805, 9223372036854775807] and 0 overlaps 3 bytes at offset 9223372036854775802" "LP64" { target lp64 } } */
398 T (d
, s
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[9223372036854775805, 9223372036854775807] overlaps 3 bytes at offset 9223372036854775802" "LP64" { target lp64 } } */
399 T (d
+ i
, s
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[9223372036854775805, 9223372036854775807] and 0 overlaps 3 bytes at offset 9223372036854775802" "LP64" { target lp64 } } */
400 #elif __SIZEOF_SIZE_T__ == 4
401 T (d
, d
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[2147483645, 2147483647] overlaps 3 bytes at offset 2147483642" "ILP32" { target ilp32 } } */
402 T (d
+ i
, d
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[2147483645, 2147483647] and 0 overlaps 3 bytes at offset 2147483642" "ILP32" { target ilp32} } */
404 T (d
, s
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[2147483645, 2147483647] overlaps 3 bytes at offset 2147483642" "ILP32" { target ilp32 } } */
405 T (d
+ i
, s
, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[2147483645, 2147483647] and 0 overlaps 3 bytes at offset 2147483642" "ILP32" { target ilp32} } */
408 ptrdiff_t j
= SR (DIFF_MAX
- 9, DIFF_MAX
- 1);
409 i
= SR (DIFF_MAX
- 5, DIFF_MAX
- 1);
413 n
= UR (4, DIFF_MAX
- 1);
416 n
= UR (4, SIZE_MAX
- 1);
419 j
= SR (DIFF_MAX
- 8, DIFF_MAX
- 1);
422 j
= SR (DIFF_MAX
- 7, DIFF_MAX
- 1);
423 T (d
+ i
, d
+ j
, n
); /* { dg-warning "accessing 4( or more)? bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and \\\[\[0-9\]+, \[0-9\]+] overlaps" "memcpy" } */
425 j
= SR (DIFF_MAX
- 6, DIFF_MAX
- 1);
426 T (d
+ i
, d
+ j
, n
); /* { dg-warning "accessing 4( or more)? bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and \\\[\[0-9\]+, \[0-9\]+] overlaps" "memcpy" } */
428 n
= UR (3, DIFF_MAX
);
431 j
= SR (DIFF_MAX
- 6, DIFF_MAX
- 1);
434 j
= SR (DIFF_MAX
- 5, DIFF_MAX
- 1);
435 T (d
+ i
, d
+ j
, n
); /* { dg-warning "accessing 3 or more bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 or more bytes" "memcpy" } */
437 j
= SR (DIFF_MAX
- 4, DIFF_MAX
- 1);
438 T (d
+ i
, d
+ j
, n
); /* { dg-warning "accessing 3 or more bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 or more bytes" "memcpy" } */
440 j
= SR (DIFF_MAX
- 2, DIFF_MAX
- 1);
441 T (d
+ i
, d
+ j
, n
); /* { dg-warning "accessing 3 or more bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and \\\[\[0-9\]+, \[0-9\]+] overlaps 1 or more bytes" "memcpy" } */
444 /* Exercise memcpy with destination and source of unknown size. */
446 void test_memcpy_var (char *d
, const char *s
)
448 size_t n
= unsigned_value ();
453 memcpy (d
, d
, n
); /* { dg-warning "source argument is the same as destination" "memcpy" } */
456 memcpy (d
, &d
[0], n
); /* { dg-warning "source argument is the same as destination" "memcpy" } */
459 memcpy (&d
[0], d
, n
); /* { dg-warning "source argument is the same as destination" "memcpy" } */
463 memcpy (d
, s
, n
); /* { dg-warning "source argument is the same as destination" "memcpy" } */
466 /* The following overlaps if n is greater than 1. */
475 s
= d
+ signed_value ();
476 memcpy (d
, s
, unsigned_value ());
496 memcpy (d
, s
, n
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
501 memcpy (d
, s
, n
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
505 memcpy (d
, s
, n
); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
509 void test_memcpy_memarrray (struct MemArrays
*p
)
512 #define T(dst, src, n) do { \
513 if (!LINE || LINE == __LINE__) { \
515 const void *ps = (src); \
516 memcpy (pd, ps, (n)); \
522 T (p
->a8
, p
->a8
+ 1, 1);
523 T (p
->a8
, p
->a8
+ 2, 2);
524 T (p
->a8
, p
->a8
+ 8, 1);
526 T (p
->a8
, p
->a8
+ 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" } */
529 /* Exercise the absence of warnings with memmove. */
531 void test_memmove (void)
546 memmove (d
+ 1, s
, 5);
551 /* Exercise strcat with constant or known arguments. */
553 void test_strcat_cst (const char *s
)
556 #define T(init, dst, src) do { \
557 if (!LINE || LINE == __LINE__) { \
560 const char *ps = (src); \
566 T ("0", a
, a
); /* { dg-warning "source argument is the same as destination" "strcat" } */
567 T ("01", a
, a
); /* { dg-warning "source argument is the same as destination" "strcat" } */
568 T ("012", a
, a
); /* { dg-warning "source argument is the same as destination" "strcat" } */
569 /* The 3 bytes "12\0" being appended to "012" overwrite the final NUL. */
570 T ("012", a
, a
+ 1); /* { dg-warning "accessing 3 bytes at offsets 0 and 1 overlaps 1 byte at offset 3" "strcat" } */
571 T ("012", a
, a
+ 2); /* { dg-warning "accessing 2 bytes at offsets 0 and 2 overlaps 1 byte at offset 3" "strcat" } */
572 /* The nul copied from a[3] to a[3] overwrites itself so this is
574 T ("012", a
, a
+ 3); /* { dg-warning "accessing 1 byte at offsets 0 and 3 overlaps 1 byte at offset 3" "strcat" } */
582 T ("0", a
+ 1, a
); /* { dg-warning "accessing 2 bytes at offsets 1 and 0 overlaps 1 byte at offset 1" "strcat" } */
585 /* The first of the two offsets in the diagnostic for strcat is that
586 of the first write into the destination, not that of the initial
587 read from it to compute its length. */
588 T ("01", a
+ 1, a
); /* { dg-warning "accessing 3 bytes at offsets 1 and 0 overlaps 1 byte at offset 2" "strcat" } */
589 T ("01", a
+ 2, a
); /* { dg-warning "accessing 3 bytes at offsets 2 and 0 overlaps 1 byte at offset 2" "strcat" } */
592 T ("012", a
+ 1, a
); /* { dg-warning "accessing 4 bytes at offsets 1 and 0 overlaps 1 byte at offset 3" "strcat" } */
593 T ("012", a
+ 2, a
); /* { dg-warning "accessing 4 bytes at offsets 2 and 0 overlaps 1 byte at offset 3" "strcat" } */
594 T ("012", a
+ 3, a
); /* { dg-warning "accessing 4 bytes at offsets 3 and 0 overlaps 1 byte at offset 3 " "strcat" } */
598 /* Verify that the obviously benign cases below aren't diagnosed. */
601 T ("01234567", a
, s
);
604 /* Exercise strcat with destination and source of unknown length. */
606 void test_strcat_var (char *d
, const char *s
)
609 #define T(dst, src) do { \
610 if (!LINE || LINE == __LINE__) { \
612 const char *ps = (src); \
618 T (d
, d
); /* { dg-warning "source argument is the same as destination" "strcat" } */
619 T (d
, d
+ 1); /* { dg-warning "accessing 2 or more bytes at offsets 0 and 1 may overlap 1 byte" "strcat" } */
620 T (d
, d
+ 2); /* { dg-warning "accessing 3 or more bytes at offsets 0 and 2 may overlap 1 byte at offset 2" "strcat" } */
621 T (d
, d
+ 999); /* { dg-warning "accessing 1000 or more bytes at offsets 0 and 999 may overlap 1 byte at offset 999" "strcat" } */
623 /* The source string must be at least 100 bytes in length for the copy
625 T (d
, d
+ -99); /* { dg-warning "accessing 100 or more bytes at offsets 0 and -99 may overlap 1 byte" "strcat" } */
627 size_t n
= unsigned_value ();
629 T (d
+ n
, d
+ n
); /* { dg-warning "\\\[-Wrestrict" "strcat" } */
631 /* Verify that the obviously benign cases below aren't diagnosed. */
639 /* Since the offset is unknown the overlap in the call below, while
640 possible, is certainly not inevitable. Conservatively, it should
641 not be diagnosed. For safety, an argument for diagnosing can be
642 made. It's a judgment call, partly determined by the effort and
643 complexity of treating this case differently from other similar
645 T (d
, d
+ n
); /* { dg-warning "may overlap" "strcat" } */
648 /* Exercise strcpy with constant or known arguments. */
650 void test_strcpy_cst (ptrdiff_t i
)
653 #define T(init, dst, src) do { \
654 if (!LINE || LINE == __LINE__) { \
657 const char *ps = (src); \
663 T ("012", a
, a
); /* { dg-warning "source argument is the same as destination" "strcpy" } */
664 T ("012", a
, a
+ 1); /* { dg-warning "accessing 3 bytes at offsets 0 and 1 overlaps 2 bytes at offset 1" "strcpy" } */
667 /* The following doesn't overlap but it should trigger -Wstringop-overflow
668 for reading past the end. */
669 T ("012", a
, a
+ sizeof a
);
671 /* The terminating nul written to d[2] overwrites s[0]. */
672 T ("0123", a
, a
+ 2); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" } */
674 /* The '5' copied from s[2] to d[2] overwrites s[0]. */
675 T ("01234", a
, a
+ 2); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" } */
677 /* This happens to be safe in GCC but it's still wrong. */
678 T ("012", a
, a
); /* { dg-warning "source argument is the same as destination" "strcpy" } */
680 T ("012", a
+ 1, a
); /* { dg-warning "accessing 4 bytes at offsets 1 and 0 overlaps 3 bytes at offset 1" "strcpy" } */
681 T ("012", a
+ 2, a
); /* { dg-warning "accessing 4 bytes at offsets 2 and 0 overlaps 2 bytes at offset 2" "strcpy" } */
682 T ("012", a
+ 3, a
); /* { dg-warning "accessing 4 bytes at offsets 3 and 0 overlaps 1 byte at offset 3" "strcpy" } */
684 /* The following doesn't overlap but it should trigger -Wstrinop-ovewrflow
685 for writing past the end. */
686 T ("012", a
+ sizeof a
, a
);
689 /* Exercise strcpy with constant or known arguments offset by a range.
690 The tests verify the use of the lower bound of the range which is
691 more restrictive than using the upper bound for positive values. */
693 void test_strcpy_range (void)
696 #define T(N, init, dst, src) \
698 if (!LINE || LINE == __LINE__) { \
701 const char *ps = (src); \
710 T (8, "0", a
+ r
, a
); /* { dg-warning "accessing between 1 and 2 bytes at offsets \\\[0, 1] and 0 overlaps up to 2 bytes at offset \\\[0, 1]" "strcpy" { xfail *-*-*} } */
713 T (8, "01", a
+ r
, a
); /* { dg-warning "accessing 3 bytes at offsets \\\[2, 5] and 0 may overlap 1 byte at offset 2" } */
714 T (8, "012", a
+ r
, a
); /* { dg-warning "accessing 4 bytes at offsets \\\[2, 5] and 0 may overlap up to 2 bytes at offset \\\[3, 2]" "strcpy" } */
716 /* The highest offset to which to copy without overflowing the 8-byte
717 destination is 3 and that overlaps 2 bytes. */
718 T (8, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 5] and 0 overlaps between 2 and 3 bytes at offset \\\[2, 3]" "strcpy" } */
720 /* With a 9-byte destination the highest offset is 4 and that still
721 overlaps 1 byte (the final NUL). */
722 T (9, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 5] and 0 overlaps between 1 and 3 bytes at offset \\\[2, 4]" "strcpy" } */
724 /* With a 10-byte buffer it's possible to copy all 5 bytes without
725 overlap at (a + 5). Copying at offsets 2 through 4 overflows
726 between 3 and 1 bytes, respectively. */
727 T (10, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[2, 5] and 0 may overlap up to 3 bytes at offset \\\[4, 2]" "strcpy" } */
731 T (8, "01", a
+ r
, a
);
732 T (8, "012", a
+ r
, a
); /* { dg-warning "accessing 4 bytes at offsets \\\[3, 4] and 0 may overlap 1 byte at offset 3" "strcpy" } */
734 /* The highest offset to which to copy without overflowing the 8-byte
735 destination is 3 and that overlaps 2 bytes. */
736 T (8, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[3, 4] and 0 overlaps 2 bytes at offset 3" "strcpy" } */
738 /* With a 9-byte destination the highest offset is 4 and that still
739 overlaps 1 byte (the final NUL). */
740 T (9, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[3, 4] and 0 overlaps between 1 and 2 bytes at offset \\\[3, 4]" "strcpy" } */
742 /* With a 10-byte buffer it's possible to copy all 5 bytes without
743 overlap at (a + 5). Copying at offsets 2 through 4 overflows
744 between 3 and 1 bytes, respectively. */
745 T (10, "0123", a
+ r
, a
); /* { dg-warning "accessing 5 bytes at offsets \\\[3, 4] and 0 overlaps between 1 and 2 bytes at offset \\\[3, 4]" "strcpy" } */
747 T (8, "01", a
, a
+ r
);
748 T (8, "012", a
, a
+ r
);
749 T (8, "0123", a
, a
+ r
);
750 T (8, "01234", a
, a
+ r
);
752 /* With the smaller offset of 3 the final NUL definitely overlaps
753 the '4' at a[3], but with the larger offset of 4 there is no
754 overlap, so the warning is a "may overlap" and the size of
755 the overlap is 1 byte. */
756 T (8, "012345", a
, a
+ r
); /* { dg-warning "accessing between 3 and 4 bytes at offsets 0 and \\\[3, 4] may overlap 1 byte at offset 3" "strcpy" } */
757 T (8, "0123456", a
, a
+ r
); /* { dg-warning "accessing between 4 and 5 bytes at offsets 0 and \\\[3, 4] may overlap up to 2 bytes at offset 3" "strcpy" } */
759 r
= SR (3, DIFF_MAX
- 3);
760 T (8, "01", a
+ r
, a
);
761 T (8, "012", a
+ r
, a
); /* { dg-warning "accessing 4 bytes at offsets \\\[3, \[0-9\]+] and 0 may overlap 1 byte at offset 3" "strcpy" } */
763 r
= SR (DIFF_MAX
- 2, DIFF_MAX
- 1);
764 T (8, "012", a
+ r
, a
); /* { dg-warning "accessing 4 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 0 overlaps" "strcpy" } */
766 /* Exercise the full range of ptrdiff_t. */
769 /* The overlap in the cases below isn't inevitable but it is diagnosed
770 because it is possible and so the code is considered unsafe. */
771 T (8, "", a
, a
+ r
); /* { dg-warning "accessing 1 byte may overlap 1 byte" "strcpy" } */
772 T (8, "0", a
+ r
, a
); /* { dg-warning "accessing 2 bytes may overlap up to 2 bytes" "strcpy" } */
773 T (8, "012", a
+ r
, a
); /* { dg-warning "accessing 4 bytes may overlap up to 4 bytes" "strcpy" } */
775 T (8, "", a
, a
+ r
); /* { dg-warning "accessing 1 byte may overlap" "strcpy" } */
776 T (8, "0", a
, a
+ r
); /* { dg-warning "accessing between 0 and 2 bytes may overlap up to 2 bytes" "strcpy" } */
777 T (8, "012", a
, a
+ r
); /* { dg-warning "accessing between 0 and 4 bytes may overlap up to 4 bytes" "strcpy" } */
780 /* Exercise strcpy with destination and/or source of unknown lengthu. */
782 void test_strcpy_var (char *d
, const char *s
)
785 #define T(dst, src) do { \
786 if (!LINE || LINE == __LINE__) { \
788 const char *ps = (src); \
796 T (d
, &d
[0]); /* { dg-warning "source argument is the same as destination" "strcpy" } */
797 T (&d
[0], d
); /* { dg-warning "source argument is the same as destination" "strcpy" } */
800 T (d
, s
); /* { dg-warning "source argument is the same as destination" "strcpy" } */
802 /* The following overlaps if *s is not nul. It arguably should be
806 /* The following overlaps if strlen (d) is greater than 1. Like
807 the above, it possibly should be diagnosed too. */
811 /* The following overlaps only if strlen (s + n) >= n so it's not
813 s
= d
+ signed_value ();
817 /* Exercise strncpy with constant or known arguments. */
819 void test_strncpy_cst (void)
822 #define T(init, dst, src, size) do { \
823 if (!LINE || LINE == __LINE__) { \
826 const char *ps = (src); \
827 strncpy (pd, ps, (size)); \
833 T ("012", a
, a
, 1); /* { dg-warning "source argument is the same as destination " "strncpy" } */
835 T ("012", a
, a
+ 1, 1);
836 T ("012", a
, a
+ 1, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and 1 overlaps 1 byte at offset 1" "strncpy" } */
837 T ("012", a
, a
+ 1, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 1 overlaps 2 bytes at offset 1" "strncpy" } */
838 T ("012", a
, a
+ 1, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
839 T ("012", a
, a
+ 1, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
840 T ("012", a
, a
+ 1, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
842 T ("012", a
, a
+ 2, 1);
843 T ("012", a
, a
+ 2, 2);
844 /* The third written byte (nul) overwrites a[2]. */
845 T ("012", a
, a
+ 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
846 T ("012", a
, a
+ 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
847 T ("012", a
, a
+ 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
849 T ("0123", a
, a
+ 2, 1);
850 T ("0123", a
, a
+ 2, 2);
851 /* The terminating nul written to a[2] overwrites s[0]. */
852 T ("0123", a
, a
+ 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
853 T ("0123", a
, a
+ 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
854 T ("0123", a
, a
+ 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
855 T ("0123", a
, a
+ 2, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
857 T ("01234", a
, a
+ 2, 1);
858 T ("01234", a
, a
+ 2, 2);
859 T ("01234", a
, a
+ 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
860 /* The '5' copied from s[2] to d[2] overwrites s[0]. */
861 T ("01234", a
, a
+ 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
862 T ("01234", a
, a
+ 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
866 /* Exercise strncpy with one or more arguments in a determinate range. */
868 void test_strncpy_range (char *d
, size_t n
)
871 #define T(init, dst, src, size) do { \
872 if (!LINE || LINE == __LINE__) { \
874 strncpy ((dst), (src), (size)); \
875 sink (a, (dst), (src)); \
882 T ("0123", a
, a
+ i
, 0);
883 T ("0123", a
, a
+ i
, 1);
884 /* Offset in the range [0, i] is represented as a PHI (&a, &a + i)
885 that the implementation isn't equipped to handle yet. */
886 T ("0123", a
, a
+ i
, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[0, 1] may overlap 1 byte at offset 1" "strncpy" { xfail *-*-* } } */
889 T ("0123", a
, a
+ i
, 0);
890 T ("0123", a
, a
+ i
, 1);
891 T ("0123", a
, a
+ i
, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and \\\[1, 5] may overlap 1 byte at offset 1" "strncpy" } */
892 T ("0123", a
, a
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and \\\[1, 5] may overlap up to 2 bytes at offset \\\[2, 1]" "strncpy" } */
893 T ("0123", a
, a
+ i
, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and \\\[1, 5] may overlap up to 3 bytes at offset \\\[3, 1]" "strncpy" } */
894 T ("0123", a
, a
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[1, 5] may overlap up to 4 bytes at offset \\\[4, 1]" "strncpy" } */
897 T ("0123", a
, a
+ i
, 0);
898 T ("0123", a
, a
+ i
, 1);
899 T ("0123", a
, a
+ i
, 2);
900 T ("0123", a
, a
+ i
, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and \\\[2, 5] may overlap 1 byte at offset 2" "strncpy" } */
901 T ("0123", a
, a
+ i
, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and \\\[2, 5] may overlap up to 2 bytes at offset \\\[3, 2]" "strncpy" } */
902 T ("0123", a
, a
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[2, 5] may overlap up to 3 bytes at offset \\\[4, 2]" "strncpy" } */
903 T ("0123", a
, a
+ i
, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and \\\[2, 5] may overlap up to 3 bytes at offset \\\[4, 2]" "strncpy" } */
906 T ("0123", a
, a
+ i
, 0);
907 T ("0123", a
, a
+ i
, 1);
908 T ("0123", a
, a
+ i
, 2);
909 T ("0123", a
, a
+ i
, 3);
910 T ("0123", a
, a
+ i
, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and \\\[3, 5] may overlap 1 byte at offset 3" "strncpy" } */
911 T ("0123", a
, a
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[3, 5] may overlap up to 2 bytes at offset \\\[4, 3]" "strncpy" } */
912 T ("0123", a
, a
+ i
, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and \\\[3, 5] may overlap up to 2 bytes at offset \\\[4, 3]" "strncpy" } */
915 T ("0123", a
, a
+ i
, 0);
916 T ("0123", a
, a
+ i
, 1);
917 T ("0123", a
, a
+ i
, 2);
918 T ("0123", a
, a
+ i
, 3);
919 T ("0123", a
, a
+ i
, 4);
920 T ("0123", a
, a
+ i
, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[4, 5] may overlap 1 byte at offset 4" "strncpy" } */
921 T ("0123", a
, a
+ i
, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and \\\[4, 5] may overlap 1 byte at offset 4" "strncpy" } */
923 /* Verify offset and size both in some range. The strncpy checking
924 is more strict than that of memcpy and triggers even when the
925 overlap is possible but not inevitable. The following overlaps
926 like so ('*' denotes the terminating NUL, '.' the appended NUL
927 that's not copied from the source):
928 a: 01234567* (also indicates offset)
930 4567* overlaps 1 at offset 4
931 4567*. overlaps 2 at offset 4
934 567*.. overlaps 1 at offset 5 */
935 T ("01234567", a
, a
+ i
, UR (4, 6)); /* { dg-warning "accessing between 4 and 6 bytes at offsets 0 and \\\[4, 5] may overlap up to 2 bytes at offset \\\[5, 4]" "strncpy" } */
937 /* Ditto for objects of unknown sizes. */
938 T ("01234567", d
, d
+ i
, UR (4, 6)); /* { dg-warning "accessing between 4 and 6 bytes at offsets 0 and \\\[4, 5] may overlap up to 2 bytes at offset \\\[5, 4]" "strncpy" } */
940 T ("01234567", a
, a
+ i
, UR (6, 7)); /* { dg-warning "accessing between 6 and 7 bytes at offsets 0 and \\\[4, 5] overlaps between 1 and 3 bytes at offset \\\[4, 5]" "strncpy" } */
942 /* The following overlaps except in the unlikely case that value ()
943 is zero, so it's diagnosed. */
944 T ("012", a
, a
, n
); /* { dg-warning "source argument is the same as destination " "strncpy" } */
948 /* Exercise strncpy with destination and source of unknown length. */
950 void test_strncpy_var (char *d
, const char *s
, size_t n
)
953 #define T(dst, src, size) do { \
954 if (!LINE || LINE == __LINE__) { \
956 const char *ps = (src); \
957 strncpy (pd, ps, (size)); \
965 T (d
, d
, 1); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
966 T (d
, d
, n
); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
969 T (d
, d
+ 1, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
971 T (d
+ 1, d
, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
981 void test_strncpy_strcpy_var (struct MemberArrays
*ar
, const char *s
)
983 /* The following is safe and should not trigger a warning. */
984 strncpy (ar
->b
, s
, sizeof ar
->b
- 1);
985 ar
->b
[sizeof ar
->b
- 1] = '\0';
986 strcpy (ar
->a
, ar
->b
);
989 /* The following is not as safe (it might overflow ar->a) but there
990 is no overlap so it also shouldn't trigger -Wrestrict. */
991 strncpy (ar
->c
, s
, sizeof ar
->c
- 1);
992 ar
->c
[sizeof ar
->c
- 1] = '\0';
993 strcpy (ar
->a
, ar
->c
);