1 /* PR middle-end/84725 - enable attribute nonstring for all narrow character
2 types
3 Verify that -Wstringop-truncation is issued for uses of arrays and
4 pointers to qualified forms of characters of all three types.
5 { dg-do compile }
6 { dg-options "-O2 -Wall -Wstringop-truncation -fno-ipa-icf" } */
7
8 #if __cplusplus
9 extern "C"
10 #endif
11 char* strncpy (char*, const char*, __SIZE_TYPE__);
12
13 #define S "1234"
14
15 struct Arrays
16 {
17 char a[4];
18 signed char b[4];
19 unsigned char c[4];
20 };
21
22 void test_arrays (struct Arrays *p, const char *s)
23 {
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" } */
31 }
32
33 struct Pointers
34 {
35 char *p;
36 signed char *q;
37 unsigned char *r;
38 };
39
40 void test_pointers (struct Pointers *p)
41 {
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" } */
45 }
46
47 struct ConstArrays
48 {
49 const char a[4];
50 const signed char b[4];
51 const unsigned char c[4];
52 };
53
54 void test_const_arrays (struct ConstArrays *p, const char *s)
55 {
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" } */
61 }
62
63 struct ConstPointers
64 {
65 const char *p;
66 const signed char *q;
67 const unsigned char *r;
68 };
69
70 void test_const_pointers (struct ConstPointers *p)
71 {
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" } */
75 }
76
77 struct VolatileArrays
78 {
79 volatile char a[4];
80 volatile signed char b[4];
81 volatile unsigned char c[4];
82 };
83
84 void test_volatile_arrays (struct VolatileArrays *p, const char *s)
85 {
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" } */
91 }
92
93 struct VolatilePointers
94 {
95 volatile char *p;
96 volatile signed char *q;
97 volatile unsigned char *r;
98 };
99
100 void test_volatile_pointers (struct VolatilePointers *p)
101 {
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" } */
105 }
106
107 struct ConstVolatileArrays
108 {
109 const volatile char a[4];
110 const volatile signed char b[4];
111 const volatile unsigned char c[4];
112 };
113
114 void test_const_volatile_arrays (struct ConstVolatileArrays *p, const char *s)
115 {
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" } */
121 }
122
123 struct ConstVolatilePointers
124 {
125 const volatile char *p;
126 const volatile signed char *q;
127 const volatile unsigned char *r;
128 };
129
130 void test_const_volatile_pointers (struct ConstVolatilePointers *p)
131 {
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" } */
135 }
136
137 /* { dg-prune-output "-Wdiscarded-qualifiers" } */