Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/torture/boxed-ptr-1.c

raw
1 /* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
2 /* { dg-require-effective-target size24plus } */
3
4 #include <stdlib.h>
5 #include "../analyzer-decls.h"
6
7 typedef struct boxed_ptr { void *value; } boxed_ptr;
8
9 boxed_ptr __attribute__((noinline))
10 boxed_malloc (size_t sz)
11 {
12 boxed_ptr result;
13 result.value = malloc (sz);
14 return result;
15 }
16
17 boxed_ptr __attribute__((noinline))
18 boxed_free (boxed_ptr ptr)
19 {
20 free (ptr.value);
21 }
22
23 const boxed_ptr boxed_null = {NULL};
24
25 boxed_ptr test_1 (int flag)
26 {
27 boxed_ptr ptr = boxed_malloc (sizeof (int));
28
29 if (flag) /* { dg-message "following 'false' branch" } */
30 if (!ptr.value)
31 return boxed_null;
32
33 *((int *)ptr.value) = 42; /* { dg-warning "dereference of possibly-NULL '\[^\n\r\]*'" } */
34
35 return ptr;
36 }
37
38 void test_2 (int flag)
39 {
40 boxed_ptr ptr;
41
42 if (flag)
43 ptr = boxed_malloc (4096);
44 else
45 ptr = boxed_malloc (1024);
46
47 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
48
49 boxed_free (ptr);
50
51 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
52 }
53
54 void test_3 (int kind)
55 {
56 boxed_ptr ptr;
57
58 switch (kind)
59 {
60 default:
61 ptr = boxed_malloc (4096);
62 break;
63 case 0:
64 ptr = boxed_malloc (128);
65 break;
66 case 1:
67 ptr = boxed_malloc (1024);
68 break;
69 case 2:
70 ptr = boxed_malloc (65536);
71 break;
72 }
73
74 __analyzer_dump_exploded_nodes (0); /* { dg-warning "4 processed enodes" } */
75
76 boxed_free (ptr);
77
78 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
79 }