* c-c++-common/Wrestrict.c (test_strcpy_range): Revert latest change.
[official-gcc.git] / gcc / testsuite / c-c++-common / Wrestrict.c
blob671497ea3e631e992d28b4eb6eb993b1eca72794
1 /* PR 35503 - Warn about restricted pointers
2 { dg-do compile }
3 { dg-options "-O2 -Wrestrict -ftrack-macro-expansion=0" } */
5 #include "../gcc.dg/range.h"
7 #if !defined LINE
8 # define LINE 0
9 #endif
11 #if __cplusplus
12 # define restrict __restrict
13 extern "C" {
14 #endif
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);
26 #if __cplusplus
27 } /* extern "C" */
28 #endif
30 void sink (void*, ...);
32 struct MemArrays
34 char a8[8];
35 char a16[16];
36 char ax[];
39 /* Exercise memcpy with constant or known arguments. */
41 void test_memcpy_cst (void *d, const void *s)
43 #undef T
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 }; \
47 void *pd = (dst); \
48 const void *ps = (src); \
49 memcpy (pd, ps, (n)); \
50 sink (a, pd, ps); \
51 } \
52 } while (0)
54 T (a, a, 0);
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
61 diagnosed. */
62 T (a, a + 1, 1); /* { dg-warning "\\\[-Wrestrict" "memcpy with a small power of 2 size" { xfail *-*-* } } */
63 T (a, a + 3, 3);
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" } */
73 sink (a);
75 __builtin_memcpy (a, a, sizeof a);
76 sink (a);
80 char a[3][7];
81 sink (a);
83 void *d = a[0];
84 const void *s = a[1];
85 memcpy (d, s, sizeof a[0]);
86 sink (&a);
88 d = a[0];
89 s = a[1];
90 /* The following is only diagnosed for sizes that aren't small
91 powers of 2. */
92 memcpy (d, s, sizeof a[0] + 2); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
93 sink (&a);
95 d = a[0] + 1;
96 s = a[1] + 1;
97 memcpy (d, s, sizeof a[0]);
98 sink (&a);
100 d = a[0] + 1;
101 s = a[1] + 1;
102 memcpy (d, s, sizeof a[0] + 2); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
103 sink (&a);
107 struct {
108 char a[7];
109 char b[7];
110 char c[7];
111 } x;
112 sink (&x);
114 void *d = x.a;
115 const void *s = x.b;
116 memcpy (d, s, sizeof x.a);
117 sink (&x);
119 d = x.a;
120 s = x.a;
121 memcpy (d, s, sizeof x.a); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
122 sink (&x);
124 d = x.a + 4;
125 s = x.b;
126 memcpy (d, s, sizeof x.a); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
127 sink (&x);
129 d = x.a + 6;
130 s = x.b;
131 memcpy (d, s, 1);
132 sink (&x);
134 d = x.a + 7;
135 s = x.b;
136 memcpy (d, s, 3); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
137 sink (&x);
139 d = x.a + 7;
140 s = x.b + 1;
141 memcpy (d, s, 1);
142 sink (&x);
144 d = x.b;
145 s = x.a;
146 memcpy (d, s, 1);
147 sink (&x);
149 d = x.b;
150 s = x.a;
151 memcpy (d, s, sizeof x.b);
152 sink (&x);
154 d = x.b + 2;
155 s = x.a + 1;
156 memcpy (d, s, sizeof x.b);
157 sink (&x);
159 d = x.b + 2;
160 s = x.a + 2;
161 memcpy (d, s, sizeof x.b);
162 sink (&x);
164 d = x.b + 2;
165 s = x.a + 3;
166 memcpy (d, s, sizeof x.b); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
167 sink (&x);
171 #undef T
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)); \
176 sink (a); \
178 } while (0)
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)
197 #undef T
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 }; \
201 void *pd = (dst); \
202 const void *ps = (src); \
203 memcpy (pd, ps, (n)); \
204 sink (a, pd, ps); \
206 } while (0)
208 ptrdiff_t ir = SR (2, 3);
209 T (a + ir, a, 0);
210 T (a + ir, a, 1);
211 T (a + ir, a, 2);
212 T (a + ir, a, 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" } */
217 T (d + ir, d, 0);
218 T (d + ir, d, 1);
219 T (d + ir, d, 2);
220 T (d + ir, d, 3);
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. */
230 T (d + sz, d, 0);
231 T (d + sz, d, 1);
232 T (d + sz, d, 9);
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" } */
258 ir = SR (2, 5);
259 T (a, a + ir, 4);
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
267 8 bytes in size
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]. */
283 size_t o = sz << 1;
284 T (d, d + o, 12345);
285 T (d + o, d, 23456);
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)
298 1 2 | ^**? 2-3 2
299 1 3 | ^ *? 1-2 3
300 2 2 | ***? 3-4 2
301 2 3 | ^**? 2-3 3
302 3 3 | ***? 3-4 3
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. */
316 T (d, d + sz, 0);
317 T (d, d + sz, 1);
318 T (d, d + sz, 9);
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
347 (DIFF_MAX). */
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);
359 T (a, a + i, n);
360 T (a + i, a, n);
361 /* Ditto for objects of unknown sizes. */
362 T (d, d + i, n);
363 T (d + i, d, n);
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
367 overlap. */
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
376 size addition. */
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} } */
406 #endif
408 ptrdiff_t j = SR (DIFF_MAX - 9, DIFF_MAX - 1);
409 i = SR (DIFF_MAX - 5, DIFF_MAX - 1);
410 n = UR (4, 5);
411 T (d + i, d + j, n);
413 n = UR (4, DIFF_MAX - 1);
414 T (d + i, d + j, n);
416 n = UR (4, SIZE_MAX - 1);
417 T (d + i, d + j, n);
419 j = SR (DIFF_MAX - 8, DIFF_MAX - 1);
420 T (d + i, d + j, n);
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);
429 T (d + i, d + j, n);
431 j = SR (DIFF_MAX - 6, DIFF_MAX - 1);
432 T (d + i, d + j, n);
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 ();
450 memcpy (d, d, 0);
451 sink (d);
453 memcpy (d, d, n); /* { dg-warning "source argument is the same as destination" "memcpy" } */
454 sink (d);
456 memcpy (d, &d[0], n); /* { dg-warning "source argument is the same as destination" "memcpy" } */
457 sink (d);
459 memcpy (&d[0], d, n); /* { dg-warning "source argument is the same as destination" "memcpy" } */
460 sink (d);
462 s = d;
463 memcpy (d, s, n); /* { dg-warning "source argument is the same as destination" "memcpy" } */
464 sink (d);
466 /* The following overlaps if n is greater than 1. */
467 s = d + 1;
468 memcpy (d, s, n);
469 sink (d);
471 s = d + n;
472 memcpy (d, s, n);
473 sink (d);
475 s = d + signed_value ();
476 memcpy (d, s, unsigned_value ());
477 sink (d);
479 s = d + 3;
480 n = 1;
481 memcpy (d, s, n);
482 sink (d);
484 s = d + 3;
485 n = 2;
486 memcpy (d, s, n);
487 sink (d);
489 s = d + 3;
490 n = 3;
491 memcpy (d, s, n);
492 sink (d);
494 s = d + 3;
495 n = 4;
496 memcpy (d, s, n); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
497 sink (d);
499 s = d + 5;
500 n = 7;
501 memcpy (d, s, n); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
503 n = UR (0, 1);
504 s = d;
505 memcpy (d, s, n); /* { dg-warning "\\\[-Wrestrict" "memcpy" } */
509 void test_memcpy_memarrray (struct MemArrays *p)
511 #undef T
512 #define T(dst, src, n) do { \
513 if (!LINE || LINE == __LINE__) { \
514 void *pd = (dst); \
515 const void *ps = (src); \
516 memcpy (pd, ps, (n)); \
517 sink (pd, ps); \
519 } while (0)
521 T (p->a8, p->a8, 0);
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)
534 char d[7];
535 sink (d);
537 const void *s = d;
538 memmove (d, s, 7);
539 sink (d);
541 s = d + 1;
542 memmove (d, s, 6);
543 sink (d);
545 s = d + 2;
546 memmove (d + 1, s, 5);
547 sink (d);
551 /* Exercise strcat with constant or known arguments. */
553 void test_strcat_cst (const char *s)
555 #undef T
556 #define T(init, dst, src) do { \
557 if (!LINE || LINE == __LINE__) { \
558 char a[9] = init; \
559 char *pd = (dst); \
560 const char *ps = (src); \
561 strcat (pd, ps); \
562 sink (a, pd, ps); \
564 } while (0)
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
573 diagnosed. */
574 T ("012", a, a + 3); /* { dg-warning "accessing 1 byte at offsets 0 and 3 overlaps 1 byte at offset 3" "strcat" } */
576 T ("012", a, a + 4);
577 T ("012", a, a + 5);
578 T ("012", a, a + 6);
579 T ("012", a, a + 7);
580 T ("012", a, a + 8);
582 T ("0", a + 1, a); /* { dg-warning "accessing 2 bytes at offsets 1 and 0 overlaps 1 byte at offset 1" "strcat" } */
583 T ("0", a + 2, a);
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" } */
590 T ("01", a + 3, a);
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" } */
595 T ("012", a + 4, a);
596 T ("012", a + 5, a);
598 /* Verify that the obviously benign cases below aren't diagnosed. */
599 T ("012", a, "012");
600 T ("012", a, s);
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)
608 #undef T
609 #define T(dst, src) do { \
610 if (!LINE || LINE == __LINE__) { \
611 char *pd = (dst); \
612 const char *ps = (src); \
613 strcat (pd, ps); \
614 sink (pd, ps); \
616 } while (0)
618 T (d, d); /* { dg-warning "source argument is the same as destination" "strcat" } */
619 T (d, d + 1); /* { dg-warning "accessing 0 or more bytes at offsets 0 and 1 may overlap 1 byte" "strcat" } */
620 T (d, d + 2); /* { dg-warning "accessing 0 or more bytes at offsets 0 and 2 may overlap 1 byte" "strcat" } */
621 T (d, d + 999); /* { dg-warning "accessing 0 or more bytes at offsets 0 and 999 may overlap 1 byte" "strcat" } */
622 T (d, d + -99); /* { dg-warning "accessing 0 or more bytes at offsets 0 and -99 may overlap 1 byte" "strcat" } */
624 size_t n = unsigned_value ();
626 T (d + n, d + n); /* { dg-warning "\\\[-Wrestrict" "strcat" } */
628 /* Verify that the obviously benign cases below aren't diagnosed. */
629 T (d, "012");
630 T (d + 1, "0123");
631 T (d + n, "01234");
632 T (d, s);
633 T (d + 1, s);
634 T (d + n, s);
636 /* Since the offset is unknown the overlap in the call below, while
637 possible, is certainly not inevitable. Conservatively, it should
638 not be diagnosed. For safety, an argument for diagnosing can be
639 made. It's a judgment call, partly determined by the effort and
640 complexity of treating this case differently from other similar
641 to it. */
642 T (d, d + n); /* { dg-warning "may overlap" "strcat" } */
645 /* Exercise strcpy with constant or known arguments. */
647 void test_strcpy_cst (ptrdiff_t i)
649 #undef T
650 #define T(init, dst, src) do { \
651 if (!LINE || LINE == __LINE__) { \
652 char a[8] = init; \
653 char *pd = (dst); \
654 const char *ps = (src); \
655 strcpy (pd, ps); \
656 sink (a, pd, ps); \
658 } while (0)
660 T ("012", a, a); /* { dg-warning "source argument is the same as destination" "strcpy" } */
661 T ("012", a, a + 1); /* { dg-warning "accessing 3 bytes at offsets 0 and 1 overlaps 2 bytes at offset 1" "strcpy" } */
662 T ("012", a, a + 2);
663 T ("012", a, a + 3);
664 /* The following doesn't overlap but it should trigger -Wstringop-overflow
665 for reading past the end. */
666 T ("012", a, a + sizeof a);
668 /* The terminating nul written to d[2] overwrites s[0]. */
669 T ("0123", a, a + 2); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" } */
671 /* The '5' copied from s[2] to d[2] overwrites s[0]. */
672 T ("01234", a, a + 2); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" } */
674 /* This happens to be safe in GCC but it's still wrong. */
675 T ("012", a, a); /* { dg-warning "source argument is the same as destination" "strcpy" } */
677 T ("012", a + 1, a); /* { dg-warning "accessing 4 bytes at offsets 1 and 0 overlaps 3 bytes at offset 1" "strcpy" } */
678 T ("012", a + 2, a); /* { dg-warning "accessing 4 bytes at offsets 2 and 0 overlaps 2 bytes at offset 2" "strcpy" } */
679 T ("012", a + 3, a); /* { dg-warning "accessing 4 bytes at offsets 3 and 0 overlaps 1 byte at offset 3" "strcpy" } */
680 T ("012", a + 4, a);
681 /* The following doesn't overlap but it should trigger -Wstrinop-ovewrflow
682 for writing past the end. */
683 T ("012", a + sizeof a, a);
686 /* Exercise strcpy with constant or known arguments offset by a range.
687 The tests verify the use of the lower bound of the range which is
688 more restrictive than using the upper bound for positive values. */
690 void test_strcpy_range (void)
692 #undef T
693 #define T(N, init, dst, src) \
694 do { \
695 if (!LINE || LINE == __LINE__) { \
696 char a[N] = init; \
697 char *pd = (dst); \
698 const char *ps = (src); \
699 strcpy (pd, ps); \
700 sink (a, pd, ps); \
702 } while (0)
704 ptrdiff_t r;
706 r = SR (0, 1);
707 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 *-*-*} } */
709 r = SR (2, 5);
710 T (8, "01", a + r, a); /* { dg-warning "accessing 3 bytes at offsets \\\[2, 5] and 0 may overlap 1 byte at offset 2" } */
711 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" } */
713 /* The highest offset to which to copy without overflowing the 8-byte
714 destination is 3 and that overlaps 2 bytes. */
715 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" } */
717 /* With a 9-byte destination the highest offset is 4 and that still
718 overlaps 1 byte (the final NUL). */
719 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" } */
721 /* With a 10-byte buffer it's possible to copy all 5 bytes without
722 overlap at (a + 5). Copying at offsets 2 through 4 overflows
723 between 3 and 1 bytes, respectively. */
724 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" } */
727 r = SR (3, 4);
728 T (8, "01", a + r, a);
729 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" } */
731 /* The highest offset to which to copy without overflowing the 8-byte
732 destination is 3 and that overlaps 2 bytes. */
733 T (8, "0123", a + r, a); /* { dg-warning "accessing 5 bytes at offsets \\\[3, 4] and 0 overlaps 2 bytes at offset 3" "strcpy" } */
735 /* With a 9-byte destination the highest offset is 4 and that still
736 overlaps 1 byte (the final NUL). */
737 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" } */
739 /* With a 10-byte buffer it's possible to copy all 5 bytes without
740 overlap at (a + 5). Copying at offsets 2 through 4 overflows
741 between 3 and 1 bytes, respectively. */
742 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" } */
744 T (8, "01", a, a + r);
745 T (8, "012", a, a + r);
746 T (8, "0123", a, a + r);
747 T (8, "01234", a, a + r);
749 /* With the smaller offset of 3 the final NUL definitely overlaps
750 the '4' at a[3], but with the larger offset of 4 there is no
751 overlap, so the warning is a "may overlap" and the size of
752 the overlap is 1 byte. */
753 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" } */
754 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" } */
756 r = SR (3, DIFF_MAX - 3);
757 T (8, "01", a + r, a);
758 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" } */
760 r = SR (DIFF_MAX - 2, DIFF_MAX - 1);
761 T (8, "012", a + r, a); /* { dg-warning "accessing 4 bytes at offsets \\\[\[0-9\]+, \[0-9\]+] and 0 overlaps" "strcpy" } */
763 /* Exercise the full range of ptrdiff_t. */
764 r = signed_value ();
766 /* The overlap in the cases below isn't inevitable but it is diagnosed
767 because it is possible and so the code is considered unsafe. */
768 T (8, "", a, a + r); /* { dg-warning "accessing 1 byte may overlap 1 byte" "strcpy" } */
769 T (8, "0", a + r, a); /* { dg-warning "accessing 2 bytes may overlap up to 2 bytes" "strcpy" } */
770 T (8, "012", a + r, a); /* { dg-warning "accessing 4 bytes may overlap up to 4 bytes" "strcpy" } */
772 T (8, "", a, a + r); /* { dg-warning "accessing 1 byte may overlap" "strcpy" } */
773 T (8, "0", a, a + r); /* { dg-warning "accessing between 0 and 2 bytes may overlap up to 2 bytes" "strcpy" } */
774 T (8, "012", a, a + r); /* { dg-warning "accessing between 0 and 4 bytes may overlap up to 4 bytes" "strcpy" } */
777 /* Exercise strcpy with destination and/or source of unknown lengthu. */
779 void test_strcpy_var (char *d, const char *s)
781 #undef T
782 #define T(dst, src) do { \
783 if (!LINE || LINE == __LINE__) { \
784 char *pd = (dst); \
785 const char *ps = (src); \
786 strcpy (pd, ps); \
787 sink (pd, ps); \
789 } while (0)
791 T (d, s);
793 T (d, &d[0]); /* { dg-warning "source argument is the same as destination" "strcpy" } */
794 T (&d[0], d); /* { dg-warning "source argument is the same as destination" "strcpy" } */
796 s = d;
797 T (d, s); /* { dg-warning "source argument is the same as destination" "strcpy" } */
799 /* The following overlaps if *s is not nul. It arguably should be
800 diagnosed. */
801 T (d, d + 1);
803 /* The following overlaps if strlen (d) is greater than 1. Like
804 the above, it possibly should be diagnosed too. */
805 int r = SR (2, 3);
806 T (d, d + r);
808 /* The following overlaps only if strlen (s + n) >= n so it's not
809 diagnosed. */
810 s = d + signed_value ();
811 T (d, s);
814 /* Exercise strncpy with constant or known arguments. */
816 void test_strncpy_cst (void)
818 #undef T
819 #define T(init, dst, src, size) do { \
820 if (!LINE || LINE == __LINE__) { \
821 char a[9] = init; \
822 char *pd = (dst); \
823 const char *ps = (src); \
824 strncpy (pd, ps, (size)); \
825 sink (a, pd, ps); \
827 } while (0)
829 T ("012", a, a, 0);
830 T ("012", a, a, 1); /* { dg-warning "source argument is the same as destination " "strncpy" } */
832 T ("012", a, a + 1, 1);
833 T ("012", a, a + 1, 2); /* { dg-warning "accessing 2 bytes at offsets 0 and 1 overlaps 1 byte at offset 1" "strncpy" } */
834 T ("012", a, a + 1, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 1 overlaps 2 bytes at offset 1" "strncpy" } */
835 T ("012", a, a + 1, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
836 T ("012", a, a + 1, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
837 T ("012", a, a + 1, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and 1 overlaps 3 bytes at offset 1" "strncpy" } */
839 T ("012", a, a + 2, 1);
840 T ("012", a, a + 2, 2);
841 /* The third written byte (nul) overwrites a[2]. */
842 T ("012", a, a + 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
843 T ("012", a, a + 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
844 T ("012", a, a + 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
846 T ("0123", a, a + 2, 1);
847 T ("0123", a, a + 2, 2);
848 /* The terminating nul written to a[2] overwrites s[0]. */
849 T ("0123", a, a + 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
850 T ("0123", a, a + 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
851 T ("0123", a, a + 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
852 T ("0123", a, a + 2, 6); /* { dg-warning "accessing 6 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
854 T ("01234", a, a + 2, 1);
855 T ("01234", a, a + 2, 2);
856 T ("01234", a, a + 2, 3); /* { dg-warning "accessing 3 bytes at offsets 0 and 2 overlaps 1 byte at offset 2" "strncpy" } */
857 /* The '5' copied from s[2] to d[2] overwrites s[0]. */
858 T ("01234", a, a + 2, 4); /* { dg-warning "accessing 4 bytes at offsets 0 and 2 overlaps 2 bytes at offset 2" "strncpy" } */
859 T ("01234", a, a + 2, 5); /* { dg-warning "accessing 5 bytes at offsets 0 and 2 overlaps 3 bytes at offset 2" "strncpy" } */
863 /* Exercise strncpy with one or more arguments in a determinate range. */
865 void test_strncpy_range (char *d, size_t n)
867 #undef T
868 #define T(init, dst, src, size) do { \
869 if (!LINE || LINE == __LINE__) { \
870 char a[9] = init; \
871 strncpy ((dst), (src), (size)); \
872 sink (a, (dst), (src)); \
874 } while (0)
876 ptrdiff_t i;
878 i = SR (0, 1);
879 T ("0123", a, a + i, 0);
880 T ("0123", a, a + i, 1);
881 /* Offset in the range [0, i] is represented as a PHI (&a, &a + i)
882 that the implementation isn't equipped to handle yet. */
883 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 *-*-* } } */
885 i = SR (1, 5);
886 T ("0123", a, a + i, 0);
887 T ("0123", a, a + i, 1);
888 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" } */
889 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" } */
890 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" } */
891 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" } */
893 i = SR (2, 5);
894 T ("0123", a, a + i, 0);
895 T ("0123", a, a + i, 1);
896 T ("0123", a, a + i, 2);
897 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" } */
898 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" } */
899 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" } */
900 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" } */
902 i = SR (3, 5);
903 T ("0123", a, a + i, 0);
904 T ("0123", a, a + i, 1);
905 T ("0123", a, a + i, 2);
906 T ("0123", a, a + i, 3);
907 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" } */
908 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" } */
909 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" } */
911 i = SR (4, 5);
912 T ("0123", a, a + i, 0);
913 T ("0123", a, a + i, 1);
914 T ("0123", a, a + i, 2);
915 T ("0123", a, a + i, 3);
916 T ("0123", a, a + i, 4);
917 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" } */
918 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" } */
920 /* Verify offset and size both in some range. The strncpy checking
921 is more strict than that of memcpy and triggers even when the
922 overlap is possible but not inevitable. The following overlaps
923 like so ('*' denotes the terminating NUL, '.' the appended NUL
924 that's not copied from the source):
925 a: 01234567* (also indicates offset)
926 i = 4: 4567 none
927 4567* overlaps 1 at offset 4
928 4567*. overlaps 2 at offset 4
929 i = 5: 567* none
930 567*. none
931 567*.. overlaps 1 at offset 5 */
932 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" } */
934 /* Ditto for objects of unknown sizes. */
935 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" } */
937 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" } */
939 /* The following overlaps except in the unlikely case that value ()
940 is zero, so it's diagnosed. */
941 T ("012", a, a, n); /* { dg-warning "source argument is the same as destination " "strncpy" } */
945 /* Exercise strncpy with destination and source of unknown length. */
947 void test_strncpy_var (char *d, const char *s, size_t n)
949 #undef T
950 #define T(dst, src, size) do { \
951 if (!LINE || LINE == __LINE__) { \
952 char *pd = (dst); \
953 const char *ps = (src); \
954 strncpy (pd, ps, (size)); \
955 sink (pd, ps); \
957 } while (0)
959 T (d, s, 1);
960 T (d, s, n);
962 T (d, d, 1); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
963 T (d, d, n); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
965 T (d, d + 1, 1);
966 T (d, d + 1, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
967 T (d + 1, d, 1);
968 T (d + 1, d, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
971 struct MemberArrays
973 char a[7];
974 char b[8];
975 char c[9];
978 void test_strncpy_strcpy_var (struct MemberArrays *ar, const char *s)
980 /* The following is safe and should not trigger a warning. */
981 strncpy (ar->b, s, sizeof ar->b - 1);
982 ar->b[sizeof ar->b - 1] = '\0';
983 strcpy (ar->a, ar->b);
984 sink (ar);
986 /* The following is not as safe (it might overflow ar->a) but there
987 is no overlap so it also shouldn't trigger -Wrestrict. */
988 strncpy (ar->c, s, sizeof ar->c - 1);
989 ar->c[sizeof ar->c - 1] = '\0';
990 strcpy (ar->a, ar->c);
991 sink (ar);