1 /* PR middle-end/10138 - warn for uninitialized arrays passed as const*
2    arguments
3    Verify that passing pointers to uninitialized objects to const
4    arguments to built-ins is diagnosed where expected.
5    { dg-do compile }
6    { dg-options "-O -Wall -ftrivial-auto-var-init=zero" }
7    { dg-require-effective-target alloca } */
8 
9 typedef __SIZE_TYPE__ size_t;
10 
11 void* alloca (size_t);
12 void* malloc (size_t);
13 void* realloc (void*, size_t);
14 
15 void* memcpy (void*, const void*, size_t);
16 char* strcpy (char*, const char*);
17 size_t strlen (const char*);
18 
19 void sink (void*);
20 
nowarn_array_memcpy(void * d,unsigned n)21 void nowarn_array_memcpy (void *d, unsigned n)
22 {
23   int a[2];
24   /* Diagnose this?  */
25   memcpy (d, a, n /* Non-constant to avoid folding into MEM_REF.  */);
26 }
27 
nowarn_array_plus_cst_memcpy(void * d,unsigned n)28 void nowarn_array_plus_cst_memcpy (void *d, unsigned n)
29 {
30   int a[3];
31   /* Diagnose this?  */
32   memcpy (d, a + 1, n);
33 }
34 
nowarn_array_plus_var_memcpy(void * d,unsigned n,int i)35 void nowarn_array_plus_var_memcpy (void *d, unsigned n, int i)
36 {
37   int a[4];
38   /* Diagnose this?  */
39   memcpy (d, a + i, n);
40 }
41 
nowarn_array_assign_memcpy(char * d,unsigned n)42 void nowarn_array_assign_memcpy (char *d, unsigned n)
43 {
44   int a[3];
45   a[1] = 3;
46   memcpy (d, a, n);
47 }
48 
nowarn_array_init_memcpy(char * d,unsigned n)49 void nowarn_array_init_memcpy (char *d, unsigned n)
50 {
51   int a[4] = { 0 };
52   memcpy (d, a, n);
53 }
54 
nowarn_array_compound_memcpy(void * d,unsigned n)55 void nowarn_array_compound_memcpy (void *d, unsigned n)
56 {
57   memcpy (d, (int[2]){ 0 }, n);
58 }
59 
nowarn_struct_assign_memcpy(void * d,unsigned n)60 void nowarn_struct_assign_memcpy (void *d, unsigned n)
61 {
62   struct S { int a, b, c, d; } s;
63   s.b = 1;
64   s.d = 2;
65   memcpy (d, &s, n);
66 }
67 
68 
nowarn_array_init_strcpy(char * d[],unsigned n)69 void nowarn_array_init_strcpy (char *d[], unsigned n)
70 {
71   char a[8] = "012";
72 
73   strcpy (d[0], a);
74   strcpy (d[1], a + 1);
75   strcpy (d[1], a + 2);
76   strcpy (d[1], a + 3);
77   strcpy (d[1], a + 4);
78   strcpy (d[1], a + 5);
79   strcpy (d[1], a + 6);
80   strcpy (d[1], a + 7);
81 }
82 
83 
nowarn_array_assign_strcpy(char * d[],unsigned n)84 void nowarn_array_assign_strcpy (char *d[], unsigned n)
85 {
86   char a[8];
87   a[0] = '0';
88   a[1] = '1';
89   a[2] = '2';
90   a[3] = '\0';
91 
92   strcpy (d[0], a);
93   strcpy (d[1], a + 1);
94   strcpy (d[1], a + 2);
95   strcpy (d[1], a + 3);
96 }
97 
warn_array_plus_cst_strcpy(char * d,unsigned n)98 void warn_array_plus_cst_strcpy (char *d, unsigned n)
99 {
100   char a[8];
101   a[0] = '1';
102   a[1] = '2';
103   a[2] = '3';
104   a[3] = '\0';
105 
106   strcpy (d, a + 4);          // { dg-warning "\\\[-Wuninitialized" }
107   strcpy (d, a + 5);          // { dg-warning "\\\[-Wuninitialized" }
108   strcpy (d, a + 6);          // { dg-warning "\\\[-Wuninitialized" }
109   strcpy (d, a + 7);          // { dg-warning "\\\[-Wuninitialized" }
110 }
111 
nowarn_array_plus_var_strcpy(char * d,int i)112 void nowarn_array_plus_var_strcpy (char *d, int i)
113 {
114   char a[8];
115   a[0] = '1';
116   a[1] = '2';
117   a[2] = '3';
118   a[3] = '\0';
119 
120   strcpy (d, a + i);
121 }
122 
123 
nowarn_array_assign_strlen(const char * s)124 size_t nowarn_array_assign_strlen (const char *s)
125 {
126   char a[8];
127   a[0] = s[0];
128   a[1] = s[1];
129   a[2] = s[2];
130   a[3] = s[3];
131 
132   size_t n = 0;
133 
134   n += strlen (a);
135   n += strlen (a + 1);
136   n += strlen (a + 2);
137   n += strlen (a + 3);
138   return n;
139 }
140 
warn_array_plus_cst_strlen(const char * s)141 size_t warn_array_plus_cst_strlen (const char *s)
142 {
143   char a[8];
144   a[0] = s[0];
145   a[1] = s[1];
146   a[2] = s[2];
147   a[3] = s[3];
148 
149   return strlen (a + 4);      // { dg-warning "\\\[-Wuninitialized" }
150 }
151 
nowarn_array_plus_var_strlen(const char * s,int i)152 size_t nowarn_array_plus_var_strlen (const char *s, int i)
153 {
154   char a[8];
155   a[0] = s[0];
156   a[1] = s[1];
157   a[2] = s[2];
158   a[3] = s[3];
159 
160   return strlen (a + i);
161 }
162 
163 
nowarn_alloca_assign_strlen(int i)164 size_t nowarn_alloca_assign_strlen (int i)
165 {
166   char *p = (char*)alloca (8);
167   p[i] = '\0';
168   return strlen (p);
169 }
170 
nowarn_alloca_escape_strlen(int i)171 size_t nowarn_alloca_escape_strlen (int i)
172 {
173   char *p = (char*)alloca (8);
174   sink (p);
175   return strlen (p);
176 }
177 
warn_alloca_strlen(void)178 size_t warn_alloca_strlen (void)
179 {
180   char *p = (char*)alloca (8);
181   return strlen (p);          // { dg-warning "\\\[-Wuninitialized" }
182 }
183 
184 
nowarn_malloc_assign_strlen(int i)185 size_t nowarn_malloc_assign_strlen (int i)
186 {
187   char *p = (char*)malloc (8);
188   p[i] = '\0';
189   return strlen (p);
190 }
191 
nowarn_malloc_escape_strlen(int i)192 size_t nowarn_malloc_escape_strlen (int i)
193 {
194   char *p = (char*)malloc (8);
195   sink (p);
196   return strlen (p);
197 }
198 
warn_malloc_strlen(void)199 size_t warn_malloc_strlen (void)
200 {
201   char *p = (char*)malloc (8);
202   return strlen (p);          // { dg-warning "\\\[-Wuninitialized" }
203 }
204 
205 
nowarn_realloc_strlen(void * p)206 size_t nowarn_realloc_strlen (void *p)
207 {
208   char *q = (char*)realloc (p, 8);
209   return strlen (q);
210 }
211 
212 
nowarn_vla_assign_strlen(int n,int i)213 size_t nowarn_vla_assign_strlen (int n, int i)
214 {
215   char vla[n];
216   vla[i] = '\0';
217   return strlen (vla);
218 }
219 
nowarn_vla_strcpy_strlen(int n,const char * s,int i)220 size_t nowarn_vla_strcpy_strlen (int n, const char *s, int i)
221 {
222   char vla[n];
223   strcpy (vla, s);
224   return strlen (vla + i);
225 }
226 
nowarn_vla_escape_strlen(int n,int i)227 size_t nowarn_vla_escape_strlen (int n, int i)
228 {
229   char vla[n];
230   sink (vla);
231   return strlen (vla);
232 }
233 
warn_vla_strlen(unsigned n)234 size_t warn_vla_strlen (unsigned n)
235 {
236   char vla[n];
237   return strlen (vla);        // { dg-warning "\\\[-Wuninitialized" }
238 }
239