Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/realloc-4.c

raw
1 #include "../../gcc.dg/analyzer/analyzer-decls.h"
2
3 typedef __SIZE_TYPE__ size_t;
4
5
6 extern void *calloc (size_t __nmemb, size_t __size)
7 __attribute__ ((__nothrow__ , __leaf__))
8 __attribute__ ((__malloc__))
9 __attribute__ ((__alloc_size__ (1, 2))) ;
10 extern void *malloc (size_t __size)
11 __attribute__ ((__nothrow__ , __leaf__))
12 __attribute__ ((__malloc__))
13 __attribute__ ((__alloc_size__ (1)));
14 extern void *realloc (void *__ptr, size_t __size)
15 __attribute__ ((__nothrow__ , __leaf__))
16 __attribute__ ((__warn_unused_result__))
17 __attribute__ ((__alloc_size__ (2)));
18 extern void free (void *__ptr)
19 __attribute__ ((__nothrow__ , __leaf__));
20
21 /* realloc where we don't know the original size of the region. */
22
23 char *test_8 (char *p, size_t sz)
24 {
25 char *q;
26
27 __analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(\[^\n\r\]*\\)'" } */
28
29 p[0] = 'a';
30 p[1] = 'b';
31 p[2] = 'c';
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" } */
35
36 q = (char *) realloc (p, 6);
37
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" } */
41
42 if (q)
43 {
44 q[3] = 'd';
45 q[4] = 'e';
46 q[5] = 'f';
47 if (q == p)
48 {
49 /* "realloc" success, growing the buffer in-place. */
50 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
51 __analyzer_dump_capacity (q); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)6'" } */
52 __analyzer_eval (q[0] == 'a'); /* { dg-warning "TRUE" } */
53 __analyzer_eval (q[1] == 'b'); /* { dg-warning "TRUE" } */
54 __analyzer_eval (q[2] == 'c'); /* { dg-warning "TRUE" } */
55 }
56 else
57 {
58 /* "realloc" success, moving the buffer (and thus freeing "p"). */
59 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
60 __analyzer_dump_capacity (q); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)6'" } */
61 /* We don't know how much of the buffer is copied. */
62 __analyzer_eval (q[0] == 'a'); /* { dg-warning "UNKNOWN" } */
63 __analyzer_eval (q[1] == 'b'); /* { dg-warning "UNKNOWN" } */
64 __analyzer_eval (q[2] == 'c'); /* { dg-warning "UNKNOWN" } */
65 __analyzer_eval (p[0] == 'a'); /* { dg-warning "UNKNOWN" "unknown" } */
66 /* { dg-warning "use after 'free' of 'p'" "use after free" { target *-*-* } .-1 } */
67 }
68 __analyzer_eval (q[3] == 'd'); /* { dg-warning "TRUE" } */
69 __analyzer_eval (q[4] == 'e'); /* { dg-warning "TRUE" } */
70 __analyzer_eval (q[5] == 'f'); /* { dg-warning "TRUE" } */
71 }
72 else
73 {
74 /* "realloc" failure. p should be unchanged. */
75 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
76 __analyzer_dump_capacity (q); /* { dg-warning "capacity: 'UNKNOWN\\(\[^\n\r\]*\\)'" } */
77 __analyzer_eval (p[0] == 'a'); /* { dg-warning "TRUE" } */
78 __analyzer_eval (p[1] == 'b'); /* { dg-warning "TRUE" } */
79 __analyzer_eval (p[2] == 'c'); /* { dg-warning "TRUE" } */
80 return p;
81 }
82
83 return q;
84 }