[C++ PATCH] Deprecate -ffriend-injection
[official-gcc.git] / gcc / testsuite / c-c++-common / Wrestrict.c
blob72d9a4716fb22b4de5f36a08bd606c27b5b5fa98
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 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
624 below to overlap. */
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. */
632 T (d, "012");
633 T (d + 1, "0123");
634 T (d + n, "01234");
635 T (d, s);
636 T (d + 1, s);
637 T (d + n, s);
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
644 to it. */
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)
652 #undef T
653 #define T(init, dst, src) do { \
654 if (!LINE || LINE == __LINE__) { \
655 char a[8] = init; \
656 char *pd = (dst); \
657 const char *ps = (src); \
658 strcpy (pd, ps); \
659 sink (a, pd, ps); \
661 } while (0)
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" } */
665 T ("012", a, a + 2);
666 T ("012", a, a + 3);
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" } */
683 T ("012", a + 4, a);
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)
695 #undef T
696 #define T(N, init, dst, src) \
697 do { \
698 if (!LINE || LINE == __LINE__) { \
699 char a[N] = init; \
700 char *pd = (dst); \
701 const char *ps = (src); \
702 strcpy (pd, ps); \
703 sink (a, pd, ps); \
705 } while (0)
707 ptrdiff_t r;
709 r = SR (0, 1);
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 *-*-*} } */
712 r = SR (2, 5);
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" } */
730 r = SR (3, 4);
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. */
767 r = signed_value ();
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)
784 #undef T
785 #define T(dst, src) do { \
786 if (!LINE || LINE == __LINE__) { \
787 char *pd = (dst); \
788 const char *ps = (src); \
789 strcpy (pd, ps); \
790 sink (pd, ps); \
792 } while (0)
794 T (d, s);
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" } */
799 s = d;
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
803 diagnosed. */
804 T (d, d + 1);
806 /* The following overlaps if strlen (d) is greater than 1. Like
807 the above, it possibly should be diagnosed too. */
808 int r = SR (2, 3);
809 T (d, d + r);
811 /* The following overlaps only if strlen (s + n) >= n so it's not
812 diagnosed. */
813 s = d + signed_value ();
814 T (d, s);
817 /* Exercise strncpy with constant or known arguments. */
819 void test_strncpy_cst (void)
821 #undef T
822 #define T(init, dst, src, size) do { \
823 if (!LINE || LINE == __LINE__) { \
824 char a[9] = init; \
825 char *pd = (dst); \
826 const char *ps = (src); \
827 strncpy (pd, ps, (size)); \
828 sink (a, pd, ps); \
830 } while (0)
832 T ("012", a, a, 0);
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)
870 #undef T
871 #define T(init, dst, src, size) do { \
872 if (!LINE || LINE == __LINE__) { \
873 char a[9] = init; \
874 strncpy ((dst), (src), (size)); \
875 sink (a, (dst), (src)); \
877 } while (0)
879 ptrdiff_t i;
881 i = SR (0, 1);
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 *-*-* } } */
888 i = SR (1, 5);
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" } */
896 i = SR (2, 5);
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" } */
905 i = SR (3, 5);
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" } */
914 i = SR (4, 5);
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)
929 i = 4: 4567 none
930 4567* overlaps 1 at offset 4
931 4567*. overlaps 2 at offset 4
932 i = 5: 567* none
933 567*. none
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)
952 #undef T
953 #define T(dst, src, size) do { \
954 if (!LINE || LINE == __LINE__) { \
955 char *pd = (dst); \
956 const char *ps = (src); \
957 strncpy (pd, ps, (size)); \
958 sink (pd, ps); \
960 } while (0)
962 T (d, s, 1);
963 T (d, s, n);
965 T (d, d, 1); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
966 T (d, d, n); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
968 T (d, d + 1, 1);
969 T (d, d + 1, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
970 T (d + 1, d, 1);
971 T (d + 1, d, 2); /* { dg-warning "\\\[-Wrestrict" "strncpy" } */
974 struct MemberArrays
976 char a[7];
977 char b[8];
978 char c[9];
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);
987 sink (ar);
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);
994 sink (ar);