1 /* PR middle-end/84725 - enable attribute nonstring for all narrow character
3 Verify that -Wstringop-truncation is issued for uses of arrays and
4 pointers to qualified forms of characters of all three types.
6 { dg-options "-O2 -Wall -Wstringop-truncation -fno-ipa-icf" } */
11 char* strncpy (char*, const char*, __SIZE_TYPE__
);
22 void test_arrays (struct Arrays
*p
, const char *s
)
24 /* Expect accesses to all three arrays to trigger the warning,
25 including the trailing one. The size argument is a good
26 enough indication that it is not being used as a "legacy"
27 flexible array member. */
28 strncpy (p
->a
, s
, sizeof p
->a
); /* { dg-warning "\\\[-Wstringop-truncation" } */
29 strncpy ((char*)p
->b
, s
, sizeof p
->b
); /* { dg-warning "\\\[-Wstringop-truncation" } */
30 strncpy ((char*)p
->c
, s
, sizeof p
->c
); /* { dg-warning "\\\[-Wstringop-truncation" } */
40 void test_pointers (struct Pointers
*p
)
42 strncpy (p
->p
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
43 strncpy ((char*)p
->q
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
44 strncpy ((char*)p
->r
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
50 const signed char b
[4];
51 const unsigned char c
[4];
54 void test_const_arrays (struct ConstArrays
*p
, const char *s
)
56 /* Expect accesses to all three arrays to trigger the warning,
57 including the trailing one. */
58 strncpy ((char*)p
->a
, s
, sizeof p
->a
); /* { dg-warning "\\\[-Wstringop-truncation" } */
59 strncpy ((char*)p
->b
, s
, sizeof p
->b
); /* { dg-warning "\\\[-Wstringop-truncation" } */
60 strncpy ((char*)p
->c
, s
, sizeof p
->c
); /* { dg-warning "\\\[-Wstringop-truncation" } */
67 const unsigned char *r
;
70 void test_const_pointers (struct ConstPointers
*p
)
72 strncpy ((char*)p
->p
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
73 strncpy ((char*)p
->q
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
74 strncpy ((char*)p
->r
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
80 volatile signed char b
[4];
81 volatile unsigned char c
[4];
84 void test_volatile_arrays (struct VolatileArrays
*p
, const char *s
)
86 /* Expect accesses to all three arrays to trigger the warning,
87 including the trailing one. */
88 strncpy ((char*)p
->a
, s
, sizeof p
->a
); /* { dg-warning "\\\[-Wstringop-truncation" } */
89 strncpy ((char*)p
->b
, s
, sizeof p
->b
); /* { dg-warning "\\\[-Wstringop-truncation" } */
90 strncpy ((char*)p
->c
, s
, sizeof p
->c
); /* { dg-warning "\\\[-Wstringop-truncation" } */
93 struct VolatilePointers
96 volatile signed char *q
;
97 volatile unsigned char *r
;
100 void test_volatile_pointers (struct VolatilePointers
*p
)
102 strncpy ((char*)p
->p
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
103 strncpy ((char*)p
->q
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
104 strncpy ((char*)p
->r
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
107 struct ConstVolatileArrays
109 const volatile char a
[4];
110 const volatile signed char b
[4];
111 const volatile unsigned char c
[4];
114 void test_const_volatile_arrays (struct ConstVolatileArrays
*p
, const char *s
)
116 /* Expect accesses to all three arrays to trigger the warning,
117 including the trailing one. */
118 strncpy ((char*)p
->a
, s
, sizeof p
->a
); /* { dg-warning "\\\[-Wstringop-truncation" } */
119 strncpy ((char*)p
->b
, s
, sizeof p
->b
); /* { dg-warning "\\\[-Wstringop-truncation" } */
120 strncpy ((char*)p
->c
, s
, sizeof p
->c
); /* { dg-warning "\\\[-Wstringop-truncation" } */
123 struct ConstVolatilePointers
125 const volatile char *p
;
126 const volatile signed char *q
;
127 const volatile unsigned char *r
;
130 void test_const_volatile_pointers (struct ConstVolatilePointers
*p
)
132 strncpy ((char*)p
->p
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
133 strncpy ((char*)p
->q
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
134 strncpy ((char*)p
->r
, S
, sizeof S
- 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
137 /* { dg-prune-output "-Wdiscarded-qualifiers" } */