1 /* { dg-require-effective-target alloca } */
2
3 #include <stdlib.h>
4 #include "analyzer-decls.h"
5
6 typedef unsigned __INT32_TYPE__ u32;
7
8 void
9 test_1 (void)
10 {
11 char buf[16];
12 __analyzer_dump_capacity (buf); /* { dg-warning "capacity: '\\(sizetype\\)16'" } */
13 }
14
15 void
16 test_2 (void)
17 {
18 char ch;
19 __analyzer_dump_capacity (&ch); /* { dg-warning "capacity: '\\(sizetype\\)1'" } */
20 }
21
22 struct s3 { char buf[100]; };
23
24 void
25 test_3 (void)
26 {
27 struct s3 s;
28 __analyzer_dump_capacity (&s); /* { dg-warning "capacity: '\\(sizetype\\)100'" } */
29 }
30
31 /* Capacity refers to the base region, not any offset within it. */
32
33 void
34 test_4 (void)
35 {
36 char buf[1024];
37 __analyzer_dump_capacity (buf + 100); /* { dg-warning "capacity: '\\(sizetype\\)1024'" } */
38 }
39
40 void
41 test_5 (void *p)
42 {
43 __analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(sizetype\\)'" } */
44 }
45
46 void
47 test_malloc (void)
48 {
49 void *p = malloc (1024);
50
51 __analyzer_dump_capacity (p); /* { dg-warning "capacity: '\\(size_t\\)1024'" } */
52 free (p);
53 }
54
55 void
56 test_alloca (size_t sz)
57 {
58 void *p = __builtin_alloca (sz);
59 __analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(sz_\[^\n\r\]*\\)'" } */
60 }
61
62 void
63 test_vla (size_t sz)
64 {
65 char buf[sz];
66 __analyzer_dump_capacity (buf); /* { dg-warning "capacity: 'INIT_VAL\\(sz_\[^\n\r\]*\\)'" } */
67 }
68
69 static void * __attribute__((noinline))
70 called_by_test_interproc_malloc (size_t a)
71 {
72 return malloc (a);
73 }
74
75 void *
76 test_interproc_malloc (size_t sz)
77 {
78 void *p = called_by_test_interproc_malloc (sz);
79 __analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(sz_\[^\n\r\]*\\)'" } */
80 return p;
81 }
82
83 struct s
84 {
85 u32 f1;
86 char arr[];
87 };
88
89 static struct s * __attribute__((noinline))
90 alloc_s (size_t num)
91 {
92 struct s *p = malloc (sizeof(struct s) + num);
93 return p;
94 }
95
96 struct s *
97 test_trailing_array (void)
98 {
99 struct s *p = alloc_s (5);
100 __analyzer_dump_capacity (p); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)9'" } */
101 return p;
102 }
103
104 void
105 test_unknown_arr (int p[])
106 {
107 __analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(sizetype\\)'" } */
108 }