| 1 |
/* Adapted from malloc-1.c, but wrapping the pointers in a struct. */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
extern int foo (void); |
| 6 |
extern int bar (void); |
| 7 |
extern void could_free (void *); |
| 8 |
extern void cant_free (const void *); /* since it's a const void *. */ |
| 9 |
|
| 10 |
typedef struct boxed_ptr { void *value; } boxed_ptr; |
| 11 |
|
| 12 |
boxed_ptr |
| 13 |
boxed_malloc (size_t sz) |
| 14 |
{ |
| 15 |
boxed_ptr result; |
| 16 |
result.value = malloc (sz); |
| 17 |
return result; |
| 18 |
} |
| 19 |
|
| 20 |
boxed_ptr |
| 21 |
boxed_free (boxed_ptr ptr) |
| 22 |
{ |
| 23 |
free (ptr.value); |
| 24 |
} |
| 25 |
|
| 26 |
const boxed_ptr boxed_null = {NULL}; |
| 27 |
|
| 28 |
void test_1 (void) |
| 29 |
{ |
| 30 |
boxed_ptr ptr; |
| 31 |
ptr.value = malloc (1024); |
| 32 |
free (ptr.value); |
| 33 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 34 |
} |
| 35 |
|
| 36 |
void test_2 (boxed_ptr ptr) |
| 37 |
{ |
| 38 |
free (ptr.value); |
| 39 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 40 |
} |
| 41 |
|
| 42 |
boxed_ptr |
| 43 |
test_3 (void) |
| 44 |
{ |
| 45 |
boxed_ptr ptr; |
| 46 |
ptr.value = malloc (sizeof (int)); |
| 47 |
*(int *)ptr.value = 42; /* { dg-warning "dereference of possibly-NULL 'ptr.value' \\\[CWE-690\\\]" } */ |
| 48 |
return ptr; |
| 49 |
} |
| 50 |
|
| 51 |
boxed_ptr |
| 52 |
test_4 (void) |
| 53 |
{ |
| 54 |
boxed_ptr ptr; |
| 55 |
ptr.value = malloc (sizeof (int)); |
| 56 |
int *iptr = (int *)ptr.value; |
| 57 |
if (iptr) |
| 58 |
*iptr = 42; |
| 59 |
else |
| 60 |
*iptr = 43; /* { dg-warning "dereference of NULL 'iptr' \\\[CWE-476\\\]" } */ |
| 61 |
return ptr; |
| 62 |
} |
| 63 |
|
| 64 |
int test_5 (boxed_ptr ptr) |
| 65 |
{ |
| 66 |
free (ptr.value); |
| 67 |
return *(int *)ptr.value; /* { dg-warning "use after 'free' of 'ptr.value'" } */ |
| 68 |
} |
| 69 |
|
| 70 |
void test_6 (void *ptr) |
| 71 |
{ |
| 72 |
boxed_ptr q; |
| 73 |
q.value = ptr; |
| 74 |
free (ptr); |
| 75 |
free (q.value); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 76 |
} |
| 77 |
|
| 78 |
void test_6a (boxed_ptr ptr) |
| 79 |
{ |
| 80 |
boxed_ptr q; |
| 81 |
q = ptr; |
| 82 |
boxed_free (ptr); |
| 83 |
free (q.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 84 |
} |
| 85 |
|
| 86 |
void test_7 (void) |
| 87 |
{ |
| 88 |
boxed_ptr ptr = boxed_malloc(4096); |
| 89 |
if (!ptr.value) |
| 90 |
return; |
| 91 |
__builtin_memset(ptr.value, 0, 4096); |
| 92 |
boxed_free(ptr); |
| 93 |
} |
| 94 |
|
| 95 |
boxed_ptr test_8 (void) |
| 96 |
{ |
| 97 |
boxed_ptr ptr = boxed_malloc(4096); |
| 98 |
if (!ptr.value) |
| 99 |
return boxed_null; |
| 100 |
__builtin_memset(ptr.value, 0, 4096); |
| 101 |
return ptr; |
| 102 |
} |
| 103 |
|
| 104 |
void test_9 (void) |
| 105 |
{ |
| 106 |
boxed_ptr ptr = boxed_malloc (1024); |
| 107 |
|
| 108 |
int i; |
| 109 |
for (i = 0; i < 1024; i++) |
| 110 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 111 |
} |
| 112 |
|
| 113 |
void test_10 (void) |
| 114 |
{ |
| 115 |
boxed_ptr ptr = boxed_malloc (1024); |
| 116 |
|
| 117 |
int i; |
| 118 |
for (i = 0; i < 1024; i++) |
| 119 |
foo (); |
| 120 |
|
| 121 |
free (ptr.value); |
| 122 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 123 |
} |
| 124 |
|
| 125 |
void test_11 (void) |
| 126 |
{ |
| 127 |
boxed_ptr ptr = boxed_malloc (1024); |
| 128 |
|
| 129 |
while (foo ()) |
| 130 |
bar (); |
| 131 |
|
| 132 |
free (ptr.value); |
| 133 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 134 |
} |
| 135 |
|
| 136 |
void test_12 (void) |
| 137 |
{ |
| 138 |
boxed_ptr ptr = boxed_malloc (1024); |
| 139 |
|
| 140 |
while (1) |
| 141 |
{ |
| 142 |
free (ptr.value); /* { dg-warning "infinite loop" } */ |
| 143 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void test_13 (void) |
| 148 |
{ |
| 149 |
boxed_ptr p = boxed_malloc (1024); |
| 150 |
boxed_ptr q = boxed_malloc (1024); |
| 151 |
|
| 152 |
foo (); |
| 153 |
if (!q.value) |
| 154 |
{ |
| 155 |
boxed_free (q); |
| 156 |
return; /* { dg-warning "leak of 'p.value'" } */ |
| 157 |
} |
| 158 |
bar (); |
| 159 |
boxed_free (q); |
| 160 |
boxed_free (p); |
| 161 |
} |
| 162 |
|
| 163 |
void test_14 (void) |
| 164 |
{ |
| 165 |
boxed_ptr p, q; |
| 166 |
p = boxed_malloc (1024); |
| 167 |
if (!p.value) |
| 168 |
return; |
| 169 |
|
| 170 |
q = boxed_malloc (1024); |
| 171 |
if (!q.value) |
| 172 |
{ |
| 173 |
boxed_free (p); |
| 174 |
boxed_free (q); |
| 175 |
/* oops: missing "return". */ |
| 176 |
} |
| 177 |
bar (); |
| 178 |
boxed_free (q); /* Although this looks like a double-'free' of q, |
| 179 |
it's known to be NULL for the case where free is |
| 180 |
called twice on it. */ |
| 181 |
free (p.value); /* { dg-warning "double-'free' of 'p.value'" } */ |
| 182 |
} |
| 183 |
|
| 184 |
void test_15 (void) |
| 185 |
{ |
| 186 |
boxed_ptr p, q; |
| 187 |
p.value = NULL; |
| 188 |
q.value = NULL; |
| 189 |
|
| 190 |
p = boxed_malloc (1024); |
| 191 |
if (!p.value) |
| 192 |
goto fail; |
| 193 |
|
| 194 |
foo (); |
| 195 |
|
| 196 |
q = boxed_malloc (1024); |
| 197 |
if (!q.value) |
| 198 |
goto fail; |
| 199 |
|
| 200 |
bar (); |
| 201 |
|
| 202 |
fail: |
| 203 |
boxed_free (q); |
| 204 |
boxed_free (p); |
| 205 |
} |
| 206 |
|
| 207 |
void test_16 (void) |
| 208 |
{ |
| 209 |
boxed_ptr p, q; /* { dg-message "region created on stack here" } */ |
| 210 |
|
| 211 |
p = boxed_malloc (1024); |
| 212 |
if (!p.value) |
| 213 |
goto fail; |
| 214 |
|
| 215 |
foo (); |
| 216 |
|
| 217 |
q = boxed_malloc (1024); |
| 218 |
if (!q.value) |
| 219 |
goto fail; |
| 220 |
|
| 221 |
bar (); |
| 222 |
|
| 223 |
fail: |
| 224 |
boxed_free (q); /* { dg-warning "use of uninitialized value 'q'" } */ |
| 225 |
boxed_free (p); |
| 226 |
} |
| 227 |
|
| 228 |
void test_17 (void) |
| 229 |
{ |
| 230 |
boxed_ptr ptr = boxed_malloc (1024); |
| 231 |
} /* { dg-warning "leak of 'ptr.value'" } */ |
| 232 |
|
| 233 |
void test_18 (void) |
| 234 |
{ |
| 235 |
boxed_ptr ptr = boxed_malloc (64); |
| 236 |
ptr = boxed_null; /* { dg-warning "leak of 'ptr.value'" } */ |
| 237 |
} |
| 238 |
|
| 239 |
void test_18a (void) |
| 240 |
{ |
| 241 |
boxed_ptr ptr = boxed_malloc (64); |
| 242 |
ptr.value = NULL; /* { dg-warning "leak of 'ptr.value'" } */ |
| 243 |
} |
| 244 |
|
| 245 |
void test_19 (void) |
| 246 |
{ |
| 247 |
boxed_ptr ptr = boxed_malloc (64); |
| 248 |
free (ptr.value); |
| 249 |
ptr.value = NULL; |
| 250 |
free (ptr.value); |
| 251 |
} |
| 252 |
|
| 253 |
boxed_ptr global_ptr_20; |
| 254 |
|
| 255 |
void test_20 (void) |
| 256 |
{ |
| 257 |
global_ptr_20 = boxed_malloc (1024); |
| 258 |
} |
| 259 |
|
| 260 |
int *test_21 (int i) |
| 261 |
{ |
| 262 |
boxed_ptr ptr = boxed_malloc (sizeof (int)); |
| 263 |
if (!ptr.value) |
| 264 |
abort (); |
| 265 |
*(int *)ptr.value = i; |
| 266 |
return ptr.value; |
| 267 |
} |
| 268 |
|
| 269 |
boxed_ptr test_21a (int i) |
| 270 |
{ |
| 271 |
boxed_ptr ptr = boxed_malloc (sizeof (int)); |
| 272 |
if (!ptr.value) |
| 273 |
abort (); |
| 274 |
*(int *)ptr.value = i; |
| 275 |
return ptr; |
| 276 |
} |
| 277 |
|
| 278 |
void test_22 (void) |
| 279 |
{ |
| 280 |
boxed_ptr ptr = boxed_malloc (1024); |
| 281 |
|
| 282 |
int i; |
| 283 |
for (i = 5; i < 10; i++) |
| 284 |
foo (); |
| 285 |
|
| 286 |
free (ptr.value); |
| 287 |
free (ptr.value); /* { dg-warning "double-'free' of 'ptr.value'" } */ |
| 288 |
} |
| 289 |
|
| 290 |
int test_24 (void) |
| 291 |
{ |
| 292 |
boxed_ptr ptr; |
| 293 |
ptr.value = __builtin_alloca (sizeof (int)); /* { dg-message "region created on stack here" } */ |
| 294 |
free (ptr.value); /* { dg-warning "'free' of 'ptr.value' which points to memory on the stack \\\[CWE-590\\\]" } */ |
| 295 |
} |
| 296 |
|
| 297 |
int test_25 (void) |
| 298 |
{ |
| 299 |
char tmp[100]; /* { dg-message "region created on stack here" } */ |
| 300 |
boxed_ptr p; |
| 301 |
p.value = tmp; |
| 302 |
free (p.value); /* { dg-warning "'free' of '&tmp' which points to memory on the stack \\\[CWE-590\\\]" } */ |
| 303 |
} |
| 304 |
|
| 305 |
char global_buffer[100]; /* { dg-message "region created here" } */ |
| 306 |
|
| 307 |
int test_26 (void) |
| 308 |
{ |
| 309 |
boxed_ptr p; |
| 310 |
p.value = global_buffer; |
| 311 |
free (p.value); /* { dg-warning "'free' of '&global_buffer' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 312 |
} |
| 313 |
|
| 314 |
struct coord { |
| 315 |
float x; |
| 316 |
float y; |
| 317 |
}; |
| 318 |
|
| 319 |
boxed_ptr test_27 (void) |
| 320 |
{ |
| 321 |
boxed_ptr p = boxed_malloc (sizeof (struct coord)); |
| 322 |
((struct coord *)p.value)->x = 0.f; /* { dg-warning "dereference of possibly-NULL 'p.value' \\\[CWE-690\\\]" } */ |
| 323 |
|
| 324 |
/* Only the first such usage should be reported: */ |
| 325 |
((struct coord *)p.value)->y = 0.f; |
| 326 |
|
| 327 |
return p; |
| 328 |
} |
| 329 |
|
| 330 |
struct link |
| 331 |
{ |
| 332 |
boxed_ptr m_ptr; |
| 333 |
}; |
| 334 |
|
| 335 |
boxed_ptr test_29 (void) |
| 336 |
{ |
| 337 |
boxed_ptr res = boxed_malloc (sizeof (struct link)); |
| 338 |
if (!res.value) |
| 339 |
return boxed_null; |
| 340 |
((struct link *)res.value)->m_ptr = boxed_malloc (sizeof (struct link)); |
| 341 |
return res; |
| 342 |
} |
| 343 |
|
| 344 |
void test_31 (void) |
| 345 |
{ |
| 346 |
struct link tmp; |
| 347 |
boxed_ptr ptr = boxed_malloc (sizeof (struct link)); |
| 348 |
tmp.m_ptr = ptr; |
| 349 |
} /* { dg-warning "leak" } */ |
| 350 |
|
| 351 |
void test_32 (void) |
| 352 |
{ |
| 353 |
boxed_ptr ptr = boxed_malloc (1024); |
| 354 |
could_free (ptr.value); |
| 355 |
} /* { dg-bogus "leak" } */ |
| 356 |
|
| 357 |
void test_33 (void) |
| 358 |
{ |
| 359 |
boxed_ptr ptr = boxed_malloc (1024); |
| 360 |
cant_free (ptr.value); |
| 361 |
} /* { dg-warning "leak of 'ptr.value'" } */ |
| 362 |
|
| 363 |
void test_34 (void) |
| 364 |
{ |
| 365 |
float *q; |
| 366 |
boxed_ptr p = boxed_malloc (sizeof (struct coord)); |
| 367 |
if (!p.value) |
| 368 |
return; |
| 369 |
((struct coord *)p.value)->x = 0.0f; |
| 370 |
q = &((struct coord *)p.value)->x; |
| 371 |
boxed_free (p); |
| 372 |
*q = 1.0f; /* { dg-warning "use after 'free' of 'q'" } */ |
| 373 |
}; |
| 374 |
|
| 375 |
int test_35 (void) |
| 376 |
{ |
| 377 |
boxed_ptr ptr = boxed_malloc(4096); |
| 378 |
if (!ptr.value) |
| 379 |
return -1; |
| 380 |
__builtin_memset(ptr.value, 0, 4096); |
| 381 |
boxed_free(ptr); |
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
void test_36 (void) |
| 386 |
{ |
| 387 |
boxed_ptr ptr = boxed_malloc(4096); |
| 388 |
if (!ptr.value) |
| 389 |
return; |
| 390 |
__builtin_memset(ptr.value, 0, 4096); |
| 391 |
boxed_free(ptr); |
| 392 |
} |
| 393 |
|
| 394 |
boxed_ptr test_37a (void) |
| 395 |
{ |
| 396 |
boxed_ptr ptr = boxed_malloc(4096); |
| 397 |
__builtin_memset(ptr.value, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr.value' where non-null expected \\\[CWE-690\\\]" } */ |
| 398 |
return ptr; |
| 399 |
} |
| 400 |
|
| 401 |
int test_37b (void) |
| 402 |
{ |
| 403 |
boxed_ptr p = boxed_malloc(4096); |
| 404 |
boxed_ptr q = boxed_malloc(4096); |
| 405 |
if (p.value) { |
| 406 |
__builtin_memset(p.value, 0, 4096); /* Not a bug: checked */ |
| 407 |
} else { |
| 408 |
__builtin_memset(q.value, 0, 4096); /* { dg-warning "use of possibly-NULL 'q.value' where non-null expected \\\[CWE-690\\\]" } */ |
| 409 |
} |
| 410 |
boxed_free(p); |
| 411 |
boxed_free(q); |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
extern void might_use_ptr (void *ptr); |
| 416 |
|
| 417 |
void test_38(int i) |
| 418 |
{ |
| 419 |
boxed_ptr p; |
| 420 |
|
| 421 |
p = boxed_malloc(1024); |
| 422 |
if (p.value) { |
| 423 |
boxed_free(p); |
| 424 |
might_use_ptr(p.value); /* { dg-warning "use after 'free' of 'p.value'" "" { xfail *-*-* } } */ |
| 425 |
// TODO: xfail |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
boxed_ptr |
| 430 |
test_39 (int i) |
| 431 |
{ |
| 432 |
boxed_ptr p = boxed_malloc(sizeof(int*)); |
| 433 |
*(int *)p.value = i; /* { dg-warning "dereference of possibly-NULL 'p.value' \\\[CWE-690\\\]" } */ |
| 434 |
return p; |
| 435 |
} |
| 436 |
|
| 437 |
boxed_ptr |
| 438 |
test_41 (int flag) |
| 439 |
{ |
| 440 |
boxed_ptr buffer; |
| 441 |
|
| 442 |
if (flag) { |
| 443 |
buffer = boxed_malloc(4096); |
| 444 |
} else { |
| 445 |
buffer = boxed_null; |
| 446 |
} |
| 447 |
|
| 448 |
((char *)buffer.value)[0] = 'a'; /* { dg-warning "dereference of possibly-NULL 'buffer.value' \\\[CWE-690\\\]" "possibly-NULL" } */ |
| 449 |
/* { dg-warning "dereference of NULL" "NULL" { target *-*-* } .-1 } */ |
| 450 |
|
| 451 |
return buffer; |
| 452 |
} |
| 453 |
|
| 454 |
extern void might_take_ownership (boxed_ptr ptr); |
| 455 |
|
| 456 |
void test_45 (void) |
| 457 |
{ |
| 458 |
boxed_ptr p = boxed_malloc (1024); |
| 459 |
might_take_ownership (p); |
| 460 |
} |
| 461 |
|
| 462 |
/* Free of function, and of label within function. */ |
| 463 |
|
| 464 |
void test_50a (void) |
| 465 |
{ |
| 466 |
} |
| 467 |
|
| 468 |
void test_50b (void) |
| 469 |
{ |
| 470 |
boxed_ptr ptr; |
| 471 |
ptr.value = test_50a; |
| 472 |
free (ptr.value); /* { dg-warning "'free' of '&test_50a' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 473 |
} |
| 474 |
|
| 475 |
void test_50c (void) |
| 476 |
{ |
| 477 |
my_label: |
| 478 |
boxed_ptr ptr; |
| 479 |
ptr.value = &&my_label; |
| 480 |
free (ptr.value); /* { dg-warning "'free' of '&my_label' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 481 |
} |
| 482 |
|
| 483 |
/* { dg-prune-output "\\\[-Wfree-nonheap-object" } */ |