analyzer: enable taint state machine by default [PR103533]
[official-gcc.git] / gcc / testsuite / c-c++-common / Wstringop-truncation-4.c
blob6ed6a28a7af2c5dfb6cc68ddd0aab61afe3dcd7e
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" } */
8 #if __cplusplus
9 extern "C"
10 #endif
11 char* strncpy (char*, const char*, __SIZE_TYPE__);
13 #define S "1234"
15 struct Arrays
17 char a[4];
18 signed char b[4];
19 unsigned char c[4];
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" } */
33 struct Pointers
35 char *p;
36 signed char *q;
37 unsigned char *r;
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" } */
47 struct ConstArrays
49 const char a[4];
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" } */
63 struct ConstPointers
65 const char *p;
66 const signed char *q;
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" } */
77 struct VolatileArrays
79 volatile char a[4];
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
95 volatile char *p;
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" } */