| 1 |
/* PR middle-end/10138 - warn for uninitialized arrays passed as const* |
| 2 |
arguments |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-std=gnu17 -O -Wall" } */ |
| 5 |
|
| 6 |
typedef __SIZE_TYPE__ size_t; |
| 7 |
|
| 8 |
void* alloca (size_t); |
| 9 |
void* malloc (size_t); |
| 10 |
void* realloc (void*, size_t); |
| 11 |
|
| 12 |
void fpi (int*); |
| 13 |
void fpci (const int*); |
| 14 |
void fpcv (const void*); |
| 15 |
|
| 16 |
|
| 17 |
void nowarn_scalar_fpi (void) |
| 18 |
{ |
| 19 |
int x; |
| 20 |
fpi (&x); |
| 21 |
} |
| 22 |
|
| 23 |
void nowarn_scalar_plus_cst_fpi (void) |
| 24 |
{ |
| 25 |
int x; |
| 26 |
// This deserves a warning other than -Wuninitialized. |
| 27 |
fpi (&x + 1); |
| 28 |
} |
| 29 |
|
| 30 |
void nowarn_scalar_plus_var_fpi (int i) |
| 31 |
{ |
| 32 |
int x; |
| 33 |
// Same as above, this deserves a warning other than -Wuninitialized. |
| 34 |
fpi (&x + i); |
| 35 |
} |
| 36 |
|
| 37 |
void nowarn_array_assign_fpci (void) |
| 38 |
{ |
| 39 |
int a[2]; |
| 40 |
a[0] = 0; |
| 41 |
fpci (a); |
| 42 |
} |
| 43 |
|
| 44 |
void nowarn_array_assign_plus_cst_fpci (void) |
| 45 |
{ |
| 46 |
int a[4]; |
| 47 |
a[1] = 0; |
| 48 |
a[2] = 1; |
| 49 |
fpci (a + 1); |
| 50 |
} |
| 51 |
|
| 52 |
void nowarn_array_init_fpci (void) |
| 53 |
{ |
| 54 |
int a[4] = { 0 }; |
| 55 |
fpci (a); |
| 56 |
} |
| 57 |
|
| 58 |
void nowarn_array_compound_fpi (void) |
| 59 |
{ |
| 60 |
fpi ((int[2]){ 1 }); |
| 61 |
} |
| 62 |
|
| 63 |
void nowarn_array_compound_fpci (void) |
| 64 |
{ |
| 65 |
fpci ((int[3]){ 1 }); |
| 66 |
} |
| 67 |
|
| 68 |
void warn_array_fpci (void) |
| 69 |
{ |
| 70 |
int a[4]; // { dg-message "declared here" }" |
| 71 |
fpci (a); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 72 |
} |
| 73 |
|
| 74 |
void warn_array_plus_cst_fpci (void) |
| 75 |
{ |
| 76 |
int a[4]; |
| 77 |
fpci (a + 1); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 78 |
} |
| 79 |
|
| 80 |
void warn_array_plus_var_fpci (int i) |
| 81 |
{ |
| 82 |
int a[4]; |
| 83 |
fpci (a + i); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 84 |
} |
| 85 |
|
| 86 |
void nowarn_array_end_fpci (void) |
| 87 |
{ |
| 88 |
int a[4]; |
| 89 |
/* This should be diagnosed by a warning other than -Wuninitialized |
| 90 |
because the just-past-the-end pointer cannot be dereferenced and |
| 91 |
the function doesn't take any other pointer to tell where the start |
| 92 |
of the array is. -Wuninitialized isn't appropriate because there |
| 93 |
is nothing to initialize at that offset. */ |
| 94 |
fpci (a + 4); |
| 95 |
} |
| 96 |
|
| 97 |
void warn_matrix_fpcv (void) |
| 98 |
{ |
| 99 |
int a[2][2]; |
| 100 |
fpci (a[1]); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 101 |
} |
| 102 |
|
| 103 |
void warn_scalar_fpcv (void) |
| 104 |
{ |
| 105 |
int i; |
| 106 |
fpci (&i); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 107 |
} |
| 108 |
|
| 109 |
void warn_scalar_plus_cst_fpcv (void) |
| 110 |
{ |
| 111 |
int x; |
| 112 |
/* Same as above, this deserves a warning other than -Wuninitialized |
| 113 |
for passing the function a past-the-end pointer with no other |
| 114 |
argument. */ |
| 115 |
fpci (&x + 1); |
| 116 |
} |
| 117 |
|
| 118 |
void warn_scalar_plus_var_fpcv (int i) |
| 119 |
{ |
| 120 |
int x; |
| 121 |
fpci (&x + i); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 122 |
} |
| 123 |
|
| 124 |
void nowarn_struct_assign_fpci (void) |
| 125 |
{ |
| 126 |
struct { int a, b; } s; |
| 127 |
s.a = 0; |
| 128 |
fpci (&s.a); |
| 129 |
} |
| 130 |
|
| 131 |
void warn_struct_assign_fpci (void) |
| 132 |
{ |
| 133 |
struct { int a, b; } s; |
| 134 |
s.a = 0; |
| 135 |
fpci (&s.b); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 136 |
} |
| 137 |
|
| 138 |
void nowarn_struct_init_fpci (void) |
| 139 |
{ |
| 140 |
struct { int a, b; } s = { 0 }; |
| 141 |
fpci (&s.a); |
| 142 |
fpci (&s.b); |
| 143 |
} |
| 144 |
|
| 145 |
void nowarn_struct_compound_fpci (void) |
| 146 |
{ |
| 147 |
struct S { int a, b; }; |
| 148 |
fpci (&(struct S){ }.a); |
| 149 |
fpci (&(struct S){ }.b); |
| 150 |
} |
| 151 |
|
| 152 |
/* Verify that passing a just-past-the-end pointer to a const pointer |
| 153 |
argument to a function that takes another argument is not diagnosed |
| 154 |
since the two arguments together could outline a range. */ |
| 155 |
void nowarn_fp_p (void) |
| 156 |
{ |
| 157 |
extern void fpi_pci (int*, const int*); |
| 158 |
|
| 159 |
{ |
| 160 |
int i; |
| 161 |
fpi_pci (&i, &i + 1); |
| 162 |
} |
| 163 |
{ |
| 164 |
int j; |
| 165 |
fpi_pci (&j + 1, &j + 1); |
| 166 |
} |
| 167 |
|
| 168 |
extern void fpc_pcc (char*, const char*); |
| 169 |
|
| 170 |
{ |
| 171 |
char a[2]; |
| 172 |
fpc_pcc (a, a + 2); |
| 173 |
} |
| 174 |
{ |
| 175 |
char a[3]; |
| 176 |
fpc_pcc (a, a + 3); |
| 177 |
} |
| 178 |
|
| 179 |
extern void fpcc_pcc (const char*, const char*); |
| 180 |
|
| 181 |
{ |
| 182 |
char a[4]; |
| 183 |
fpcc_pcc (a + 4, a + 4); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* Verify passing addresses of empty uninitialized objects doesn't |
| 189 |
trigger a warning. */ |
| 190 |
void nowarn_fpcEmpty (void) |
| 191 |
{ |
| 192 |
struct Empty { }; |
| 193 |
extern void fpcEmpty (const struct Empty*); |
| 194 |
|
| 195 |
/* Since Empty has no members warning for it isn't really necessary. |
| 196 |
See also PR 38908. */ |
| 197 |
struct Empty s; |
| 198 |
fpcEmpty (&s); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/* Verify passing addresses of uninitialized objects to functions |
| 203 |
declared without a proptotype doesn't trigger a warning. */ |
| 204 |
void nowarn_noproto (void) |
| 205 |
{ |
| 206 |
extern void fnoproto (); |
| 207 |
int i, a[2]; |
| 208 |
|
| 209 |
fnoproto (&i, a, a + 2); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/* Verify passing addresses of uninitialized objects to variadic |
| 214 |
functions doesn't trigger a warning. */ |
| 215 |
void nowarn_vararg (void) |
| 216 |
{ |
| 217 |
extern void fvararg (int, ...); |
| 218 |
|
| 219 |
int i, a[2]; |
| 220 |
|
| 221 |
fvararg (0, &i, a, a + 2); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void nowarn_alloca_assign_fpci (unsigned n) |
| 226 |
{ |
| 227 |
int *p = (int*)alloca (n); |
| 228 |
p[0] = 0; |
| 229 |
fpci (p); |
| 230 |
} |
| 231 |
|
| 232 |
void nowarn_alloca_assign_plus_cst_fpci (unsigned n) |
| 233 |
{ |
| 234 |
int *p = (int*)alloca (n); |
| 235 |
p[1] = 0; |
| 236 |
p[2] = 1; |
| 237 |
fpci (p + 1); |
| 238 |
} |
| 239 |
|
| 240 |
void warn_alloca_fpci (unsigned n) |
| 241 |
{ |
| 242 |
int *p = (int*)alloca (n); |
| 243 |
fpci (p); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 244 |
} |
| 245 |
|
| 246 |
void warn_alloca_assign_plus_cst_fpci (unsigned n) |
| 247 |
{ |
| 248 |
int *p = (int*)alloca (n); |
| 249 |
p[1] = 0; |
| 250 |
p[2] = 1; |
| 251 |
fpci (p + 3); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
void nowarn_vla_assign_fpci (unsigned n) |
| 256 |
{ |
| 257 |
int a[n]; |
| 258 |
a[0] = 0; |
| 259 |
fpci (a); |
| 260 |
} |
| 261 |
|
| 262 |
void nowarn_vla_assign_plus_cst_fpci (unsigned n) |
| 263 |
{ |
| 264 |
int vla[n]; |
| 265 |
vla[1] = 0; |
| 266 |
vla[2] = 1; |
| 267 |
fpci (vla + 1); |
| 268 |
} |
| 269 |
|
| 270 |
void warn_vla_fpci (unsigned n) |
| 271 |
{ |
| 272 |
int vla[n]; // { dg-message "declared here" "pr?????" { xfail *-*-* } }" |
| 273 |
fpci (vla); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 274 |
} |
| 275 |
|
| 276 |
void warn_vla_assign_plus_cst_fpci (unsigned n) |
| 277 |
{ |
| 278 |
int vla[n]; // { dg-message "declared here" "pr?????" { xfail *-*-* } }" |
| 279 |
vla[1] = 0; |
| 280 |
vla[2] = 1; |
| 281 |
fpci (vla + 3); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
void nowarn_malloc_assign_fpci (unsigned n) |
| 286 |
{ |
| 287 |
int *p = (int*)malloc (n); |
| 288 |
p[0] = 0; |
| 289 |
fpci (p); |
| 290 |
} |
| 291 |
|
| 292 |
void nowarn_malloc_assign_plus_cst_fpci (unsigned n) |
| 293 |
{ |
| 294 |
int *p = (int*)malloc (n); |
| 295 |
p[1] = 0; |
| 296 |
p[2] = 1; |
| 297 |
fpci (p + 1); |
| 298 |
} |
| 299 |
|
| 300 |
void warn_malloc_fpci (unsigned n) |
| 301 |
{ |
| 302 |
int *p = (int*)malloc (n); |
| 303 |
fpci (p); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 304 |
} |
| 305 |
|
| 306 |
void warn_malloc_assign_plus_cst_fpci (unsigned n) |
| 307 |
{ |
| 308 |
int *p = (int*)malloc (n); // { dg-message "allocated here" "pr?????" { xfail *-*-* } }" |
| 309 |
p[1] = 0; |
| 310 |
p[2] = 1; |
| 311 |
fpci (p + 3); // { dg-warning "\\\[-Wmaybe-uninitialized" } |
| 312 |
} |