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" } */
4 
5 typedef __SIZE_TYPE__ size_t;
6 
7 extern char* strncpy (char*, const char*, size_t);
8 
9 extern char a3[3], b3[3];
10 extern char a5[5], b5[5];
11 extern char ax[], bx[];
12 
13 struct SA
14 {
15   char a3[3], b3[3];
16   char a5[5], b5[5];
17   char ax[];
18 };
19 
20 void sink (void*, ...);
21 
22 #define T(d, s, n)   sink (strncpy (d, s, n))
23 
test_array(unsigned n)24 void test_array (unsigned n)
25 {
26   T (a3, b3, 3);
27   /* For the following statemenmt, GCC 8.1 issues warning:
28 
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?
31 
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" } */
35 
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" } */
38 
39   char an[n], bn[n];
40   sink (an, bn);
41 
42   T (an, bn, sizeof bn);    /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
43 }
44 
test_member_array(struct SA * sa,unsigned n)45 void test_member_array (struct SA *sa, unsigned n)
46 {
47   T (sa->a3, sa->b3, 3);
48   T (sa->a3, sa->b3, sizeof sa->b3);  /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
49 
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" } */
52 
53   struct VarLenStruct {
54     char an[n], bn[n];
55   } x;
56 
57   sink (&x);
58   T (x.an, x.bn, sizeof x.bn);        /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */
59 }
60