Mercurial > hg > CbC > CbC_gcc
comparison gcc/testsuite/c-c++-common/Wstringop-truncation-4.c @ 131:84e7813d76e9
gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 07:37:49 +0900 |
parents | |
children | 1830386684a0 |
comparison
equal
deleted
inserted
replaced
111:04ced10e8804 | 131:84e7813d76e9 |
---|---|
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" } */ | |
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 strncpy (p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
25 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
26 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ | |
27 } | |
28 | |
29 struct Pointers | |
30 { | |
31 char *p; | |
32 signed char *q; | |
33 unsigned char *r; | |
34 }; | |
35 | |
36 void test_pointers (struct Pointers *p) | |
37 { | |
38 strncpy (p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
39 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
40 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
41 } | |
42 | |
43 struct ConstArrays | |
44 { | |
45 const char a[4]; | |
46 const signed char b[4]; | |
47 const unsigned char c[4]; | |
48 }; | |
49 | |
50 void test_const_arrays (struct ConstArrays *p, const char *s) | |
51 { | |
52 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
53 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
54 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ | |
55 } | |
56 | |
57 struct ConstPointers | |
58 { | |
59 const char *p; | |
60 const signed char *q; | |
61 const unsigned char *r; | |
62 }; | |
63 | |
64 void test_const_pointers (struct ConstPointers *p) | |
65 { | |
66 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
67 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
68 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
69 } | |
70 | |
71 struct VolatileArrays | |
72 { | |
73 volatile char a[4]; | |
74 volatile signed char b[4]; | |
75 volatile unsigned char c[4]; | |
76 }; | |
77 | |
78 void test_volatile_arrays (struct VolatileArrays *p, const char *s) | |
79 { | |
80 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
81 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
82 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ | |
83 } | |
84 | |
85 struct VolatilePointers | |
86 { | |
87 volatile char *p; | |
88 volatile signed char *q; | |
89 volatile unsigned char *r; | |
90 }; | |
91 | |
92 void test_volatile_pointers (struct VolatilePointers *p) | |
93 { | |
94 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
95 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
96 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
97 } | |
98 | |
99 struct ConstVolatileArrays | |
100 { | |
101 const volatile char a[4]; | |
102 const volatile signed char b[4]; | |
103 const volatile unsigned char c[4]; | |
104 }; | |
105 | |
106 void test_const_volatile_arrays (struct ConstVolatileArrays *p, const char *s) | |
107 { | |
108 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
109 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
110 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ | |
111 } | |
112 | |
113 struct ConstVolatilePointers | |
114 { | |
115 const volatile char *p; | |
116 const volatile signed char *q; | |
117 const volatile unsigned char *r; | |
118 }; | |
119 | |
120 void test_const_volatile_pointers (struct ConstVolatilePointers *p) | |
121 { | |
122 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
123 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
124 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */ | |
125 } | |
126 | |
127 /* { dg-prune-output "-Wdiscarded-qualifiers" } */ |