PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-truncation-3.c
blob57f4d64188a6a875ab9f96ddb63be6e9c82ee7ea
1 /* PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source
2 { dg-do compile }
3 { dg-options "-O2 -Wall -Wstringop-truncation -ftrack-macro-expansion=0" } */
5 typedef __SIZE_TYPE__ size_t;
7 extern char* strncpy (char*, const char*, size_t);
9 extern char a3[3], b3[3];
10 extern char a5[5], b5[5];
11 extern char ax[], bx[];
13 struct SA
15 char a3[3], b3[3];
16 char a5[5], b5[5];
17 char ax[];
20 void sink (void*, ...);
22 #define T(d, s, n) sink (strncpy (d, s, n))
24 void test_array (unsigned n)
26 T (a3, b3, 3);
27 /* For the following statemenmt, GCC 8.1 issues warning:
29 argument to ‘sizeof’ in ‘strncpy’ call is the same expression
30 as the source; did you mean to use the size of the destination?
32 Since the size of both the source and destination the warning
33 isn't helpful. Verify that it isn't issued. */
34 T (a3, b3, sizeof b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
36 T (a3, ax, sizeof a3); /* { dg-warning "\\\[-Wstringop-truncation" } */
37 T (ax, a3, sizeof a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */
39 char an[n], bn[n];
40 sink (an, bn);
42 T (an, bn, sizeof bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
45 void test_member_array (struct SA *sa, unsigned n)
47 T (sa->a3, sa->b3, 3);
48 T (sa->a3, sa->b3, sizeof sa->b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
50 T (sa->a3, sa->ax, sizeof sa->a3); /* { dg-warning "\\\[-Wstringop-truncation" } */
51 T (sa->ax, sa->a3, sizeof sa->a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */
53 struct VarLenStruct {
54 char an[n], bn[n];
55 } x;
57 sink (&x);
58 T (x.an, x.bn, sizeof x.bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */