| 1 |
/* { dg-additional-options "-fno-analyzer-suppress-followups" } */ |
| 2 |
|
| 3 |
#include "../../gcc.dg/analyzer/analyzer-decls.h" |
| 4 |
|
| 5 |
typedef __SIZE_TYPE__ size_t; |
| 6 |
|
| 7 |
|
| 8 |
extern void *calloc (size_t __nmemb, size_t __size) |
| 9 |
__attribute__ ((__nothrow__ , __leaf__)) |
| 10 |
__attribute__ ((__malloc__)) |
| 11 |
__attribute__ ((__alloc_size__ (1, 2))) ; |
| 12 |
extern void *malloc (size_t __size) |
| 13 |
__attribute__ ((__nothrow__ , __leaf__)) |
| 14 |
__attribute__ ((__malloc__)) |
| 15 |
__attribute__ ((__alloc_size__ (1))); |
| 16 |
extern void *realloc (void *__ptr, size_t __size) |
| 17 |
__attribute__ ((__nothrow__ , __leaf__)) |
| 18 |
__attribute__ ((__warn_unused_result__)) |
| 19 |
__attribute__ ((__alloc_size__ (2))); |
| 20 |
extern void free (void *__ptr) |
| 21 |
__attribute__ ((__nothrow__ , __leaf__)); |
| 22 |
|
| 23 |
/* realloc of calloc buffer. */ |
| 24 |
|
| 25 |
char *test_8 (size_t sz) |
| 26 |
{ |
| 27 |
char *p, *q; |
| 28 |
|
| 29 |
p = (char *) calloc (1, 3); |
| 30 |
if (!p) |
| 31 |
return NULL; |
| 32 |
|
| 33 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)3'" } */ |
| 34 |
|
| 35 |
__analyzer_eval (p[0] == 0); /* { dg-warning "TRUE" } */ |
| 36 |
__analyzer_eval (p[1] == 0); /* { dg-warning "TRUE" } */ |
| 37 |
__analyzer_eval (p[2] == 0); /* { dg-warning "TRUE" } */ |
| 38 |
|
| 39 |
q = (char *) realloc (p, 6); |
| 40 |
|
| 41 |
/* We should have 3 nodes, corresponding to "failure", |
| 42 |
"success without moving", and "success with moving". */ |
| 43 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "3 processed enodes" } */ |
| 44 |
|
| 45 |
if (q) |
| 46 |
{ |
| 47 |
__analyzer_dump_capacity (q); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)6'" } */ |
| 48 |
q[3] = 'd'; |
| 49 |
q[4] = 'e'; |
| 50 |
q[5] = 'f'; |
| 51 |
if (q == p) |
| 52 |
{ |
| 53 |
/* "realloc" success, growing the buffer in-place. */ |
| 54 |
__analyzer_eval (p[0] == 0); /* { dg-warning "TRUE" } */ |
| 55 |
__analyzer_eval (p[1] == 0); /* { dg-warning "TRUE" } */ |
| 56 |
__analyzer_eval (p[2] == 0); /* { dg-warning "TRUE" } */ |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
/* "realloc" success, moving the buffer (and thus freeing "p"). */ |
| 61 |
__analyzer_eval (q[0] == 0); /* { dg-warning "TRUE" } */ |
| 62 |
__analyzer_eval (q[1] == 0); /* { dg-warning "TRUE" } */ |
| 63 |
__analyzer_eval (q[2] == 0); /* { dg-warning "TRUE" } */ |
| 64 |
__analyzer_eval (p[0] == 'a'); /* { dg-warning "UNKNOWN" "unknown" } */ |
| 65 |
/* { dg-warning "use after 'free' of 'p'" "use after free" { target *-*-* } .-1 } */ |
| 66 |
} |
| 67 |
__analyzer_eval (q[3] == 'd'); /* { dg-warning "TRUE" } */ |
| 68 |
__analyzer_eval (q[4] == 'e'); /* { dg-warning "TRUE" } */ |
| 69 |
__analyzer_eval (q[5] == 'f'); /* { dg-warning "TRUE" } */ |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
/* "realloc" failure. p should be unchanged. */ |
| 74 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)3'" } */ |
| 75 |
__analyzer_eval (p[0] == 0); /* { dg-warning "TRUE" } */ |
| 76 |
__analyzer_eval (p[1] == 0); /* { dg-warning "TRUE" } */ |
| 77 |
__analyzer_eval (p[2] == 0); /* { dg-warning "TRUE" } */ |
| 78 |
return p; |
| 79 |
} |
| 80 |
|
| 81 |
return q; |
| 82 |
} |