Skip analyzer strndup test on hppa*-*-hpux*
[official-gcc.git] / gcc / testsuite / gcc.dg / auto-init-uninit-36.c
blob64377d380ee914b0c9fd2cf5512d7e670af335ea
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 } */
9 typedef __SIZE_TYPE__ size_t;
11 void* alloca (size_t);
12 void* malloc (size_t);
13 void* realloc (void*, size_t);
15 void* memcpy (void*, const void*, size_t);
16 char* strcpy (char*, const char*);
17 size_t strlen (const char*);
19 void sink (void*);
21 void nowarn_array_memcpy (void *d, unsigned n)
23 int a[2];
24 /* Diagnose this? */
25 memcpy (d, a, n /* Non-constant to avoid folding into MEM_REF. */);
28 void nowarn_array_plus_cst_memcpy (void *d, unsigned n)
30 int a[3];
31 /* Diagnose this? */
32 memcpy (d, a + 1, n);
35 void nowarn_array_plus_var_memcpy (void *d, unsigned n, int i)
37 int a[4];
38 /* Diagnose this? */
39 memcpy (d, a + i, n);
42 void nowarn_array_assign_memcpy (char *d, unsigned n)
44 int a[3];
45 a[1] = 3;
46 memcpy (d, a, n);
49 void nowarn_array_init_memcpy (char *d, unsigned n)
51 int a[4] = { 0 };
52 memcpy (d, a, n);
55 void nowarn_array_compound_memcpy (void *d, unsigned n)
57 memcpy (d, (int[2]){ 0 }, n);
60 void nowarn_struct_assign_memcpy (void *d, unsigned n)
62 struct S { int a, b, c, d; } s;
63 s.b = 1;
64 s.d = 2;
65 memcpy (d, &s, n);
69 void nowarn_array_init_strcpy (char *d[], unsigned n)
71 char a[8] = "012";
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);
84 void nowarn_array_assign_strcpy (char *d[], unsigned n)
86 char a[8];
87 a[0] = '0';
88 a[1] = '1';
89 a[2] = '2';
90 a[3] = '\0';
92 strcpy (d[0], a);
93 strcpy (d[1], a + 1);
94 strcpy (d[1], a + 2);
95 strcpy (d[1], a + 3);
98 void warn_array_plus_cst_strcpy (char *d, unsigned n)
100 char a[8];
101 a[0] = '1';
102 a[1] = '2';
103 a[2] = '3';
104 a[3] = '\0';
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" }
112 void nowarn_array_plus_var_strcpy (char *d, int i)
114 char a[8];
115 a[0] = '1';
116 a[1] = '2';
117 a[2] = '3';
118 a[3] = '\0';
120 strcpy (d, a + i);
124 size_t nowarn_array_assign_strlen (const char *s)
126 char a[8];
127 a[0] = s[0];
128 a[1] = s[1];
129 a[2] = s[2];
130 a[3] = s[3];
132 size_t n = 0;
134 n += strlen (a);
135 n += strlen (a + 1);
136 n += strlen (a + 2);
137 n += strlen (a + 3);
138 return n;
141 size_t warn_array_plus_cst_strlen (const char *s)
143 char a[8];
144 a[0] = s[0];
145 a[1] = s[1];
146 a[2] = s[2];
147 a[3] = s[3];
149 return strlen (a + 4); // { dg-warning "\\\[-Wuninitialized" }
152 size_t nowarn_array_plus_var_strlen (const char *s, int i)
154 char a[8];
155 a[0] = s[0];
156 a[1] = s[1];
157 a[2] = s[2];
158 a[3] = s[3];
160 return strlen (a + i);
164 size_t nowarn_alloca_assign_strlen (int i)
166 char *p = (char*)alloca (8);
167 p[i] = '\0';
168 return strlen (p);
171 size_t nowarn_alloca_escape_strlen (int i)
173 char *p = (char*)alloca (8);
174 sink (p);
175 return strlen (p);
178 size_t warn_alloca_strlen (void)
180 char *p = (char*)alloca (8);
181 return strlen (p); // { dg-warning "\\\[-Wuninitialized" }
185 size_t nowarn_malloc_assign_strlen (int i)
187 char *p = (char*)malloc (8);
188 p[i] = '\0';
189 return strlen (p);
192 size_t nowarn_malloc_escape_strlen (int i)
194 char *p = (char*)malloc (8);
195 sink (p);
196 return strlen (p);
199 size_t warn_malloc_strlen (void)
201 char *p = (char*)malloc (8);
202 return strlen (p); // { dg-warning "\\\[-Wuninitialized" }
206 size_t nowarn_realloc_strlen (void *p)
208 char *q = (char*)realloc (p, 8);
209 return strlen (q);
213 size_t nowarn_vla_assign_strlen (int n, int i)
215 char vla[n];
216 vla[i] = '\0';
217 return strlen (vla);
220 size_t nowarn_vla_strcpy_strlen (int n, const char *s, int i)
222 char vla[n];
223 strcpy (vla, s);
224 return strlen (vla + i);
227 size_t nowarn_vla_escape_strlen (int n, int i)
229 char vla[n];
230 sink (vla);
231 return strlen (vla);
234 size_t warn_vla_strlen (unsigned n)
236 char vla[n];
237 return strlen (vla); // { dg-warning "\\\[-Wuninitialized" }