libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-truncation-3.c
blob6c679747e7096c2e5cfac24ecbd9c8a2ce94970e
1 /* PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source
2 { dg-do compile }
3 { dg-require-effective-target alloca }
4 { dg-options "-O2 -Wall -Wstringop-truncation -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__ size_t;
8 extern char* strncpy (char*, const char*, size_t);
10 extern char a3[3], b3[3];
11 extern char a5[5], b5[5];
12 extern char ax[], bx[];
14 struct SA
16 char a3[3], b3[3];
17 char a5[5], b5[5];
18 char ax[];
21 void sink (void*, ...);
23 #define T(d, s, n) sink (strncpy (d, s, n))
25 void test_array (unsigned n)
27 T (a3, b3, 3);
28 /* For the following statemenmt, GCC 8.1 issues warning:
30 argument to ‘sizeof’ in ‘strncpy’ call is the same expression
31 as the source; did you mean to use the size of the destination?
33 Since the size of both the source and destination the warning
34 isn't helpful. Verify that it isn't issued. */
35 T (a3, b3, sizeof b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
37 T (a3, ax, sizeof a3); /* { dg-warning "\\\[-Wstringop-truncation" } */
38 T (ax, a3, sizeof a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */
40 char an[n], bn[n];
41 sink (an, bn);
43 T (an, bn, sizeof bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
46 void test_member_array (struct SA *sa, unsigned n)
48 T (sa->a3, sa->b3, 3);
49 T (sa->a3, sa->b3, sizeof sa->b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
51 T (sa->a3, sa->ax, sizeof sa->a3); /* { dg-warning "\\\[-Wstringop-truncation" } */
52 T (sa->ax, sa->a3, sizeof sa->a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */
54 struct VarLenStruct {
55 char an[n], bn[n];
56 } x;
58 sink (&x);
59 T (x.an, x.bn, sizeof x.bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */