1 /* { dg-additional-options "-fno-analyzer-suppress-followups" } */
3 #include "../../gcc.dg/analyzer/analyzer-decls.h"
5 typedef __SIZE_TYPE__
size_t;
7 extern void *malloc (size_t __size
)
8 __attribute__ ((__nothrow__
, __leaf__
))
9 __attribute__ ((__malloc__
))
10 __attribute__ ((__alloc_size__ (1)));
11 extern void *realloc (void *__ptr
, size_t __size
)
12 __attribute__ ((__nothrow__
, __leaf__
))
13 __attribute__ ((__warn_unused_result__
))
14 __attribute__ ((__alloc_size__ (2)));
15 extern void free (void *__ptr
)
16 __attribute__ ((__nothrow__
, __leaf__
));
18 char *test_8 (size_t sz
)
22 p
= (char *) malloc (3);
26 __analyzer_dump_capacity (p
); /* { dg-warning "capacity: '\\(size_t\\)3'" } */
32 __analyzer_eval (p
[0] == 'a'); /* { dg-warning "TRUE" } */
33 __analyzer_eval (p
[1] == 'b'); /* { dg-warning "TRUE" } */
34 __analyzer_eval (p
[2] == 'c'); /* { dg-warning "TRUE" } */
36 q
= (char *) realloc (p
, 6);
38 /* We should have 3 nodes, corresponding to "failure",
39 "success without moving", and "success with moving". */
40 __analyzer_dump_exploded_nodes (0); /* { dg-warning "3 processed enodes" } */
44 __analyzer_dump_capacity (q
); /* { dg-warning "capacity: '\\(size_t\\)6'" } */
50 /* "realloc" success, growing the buffer in-place. */
51 __analyzer_eval (p
[0] == 'a'); /* { dg-warning "TRUE" } */
52 __analyzer_eval (p
[1] == 'b'); /* { dg-warning "TRUE" } */
53 __analyzer_eval (p
[2] == 'c'); /* { dg-warning "TRUE" } */
58 /* "realloc" success, moving the buffer (and thus freeing "p"). */
59 __analyzer_eval (q
[0] == 'a'); /* { dg-warning "TRUE" } */
60 __analyzer_eval (q
[1] == 'b'); /* { dg-warning "TRUE" } */
61 __analyzer_eval (q
[2] == 'c'); /* { dg-warning "TRUE" } */
62 __analyzer_eval (p
[0] == 'a'); /* { dg-warning "UNKNOWN" "unknown" } */
63 /* { dg-warning "use after 'free' of 'p'" "use after free" { target *-*-* } .-1 } */
65 __analyzer_eval (q
[3] == 'd'); /* { dg-warning "TRUE" } */
66 __analyzer_eval (q
[4] == 'e'); /* { dg-warning "TRUE" } */
67 __analyzer_eval (q
[5] == 'f'); /* { dg-warning "TRUE" } */
71 /* "realloc" failure. p should be unchanged. */
72 __analyzer_dump_capacity (p
); /* { dg-warning "capacity: '\\(size_t\\)3'" } */
73 __analyzer_eval (p
[0] == 'a'); /* { dg-warning "TRUE" } */
74 __analyzer_eval (p
[1] == 'b'); /* { dg-warning "TRUE" } */
75 __analyzer_eval (p
[2] == 'c'); /* { dg-warning "TRUE" } */