Debian Codegraph @

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

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 /* Various tests of memory leak detection. */
4
5 #include <stdlib.h>
6
7 /* Example of an leak due to incomplete cleanup when freeing a struct. */
8
9 struct s1
10 {
11 void *ptr;
12 };
13
14 void test_1 (void)
15 {
16 struct s1 *a = (struct s1 *) malloc (sizeof (struct s1));
17 if (!a)
18 return;
19 a->ptr = malloc (1024); /* { dg-message "allocated here" } */
20 free (a); /* { dg-warning "leak of '<unknown>'" } */
21 /* TODO: we should print "a->ptr' here, rather than '<unknown>'
22 (PR analyzer/99771). */
23 }
24
25
26 /* Examples involving arrays. */
27
28 struct s2
29 {
30 void *m_arr[10];
31 };
32
33 void test_2a (void)
34 {
35 struct s2 arr[5];
36 arr[3].m_arr[4] = malloc (1024); /* { dg-message "allocated here" } */
37 } /* { dg-warning "leak of 'arr\\\[3\\\].m_arr\\\[4\\\]'" "" { target c } } */
38 /* { dg-warning "leak of 'arr\\\[3\\\].s2::m_arr\\\[4\\\]'" "" { target c++ } .-1 } */
39
40 void test_2b (int i)
41 {
42 struct s2 arr[5];
43 arr[3].m_arr[i] = malloc (1024); /* { dg-message "allocated here" } */
44 } /* { dg-warning "leak of 'arr\\\[3\\\].m_arr\\\[i\\\]'" "" { target c } } */
45 /* { dg-warning "leak of 'arr\\\[3\\\].s2::m_arr\\\[i\\\]'" "" { target c++ } .-1 } */
46
47 void test_2c (int i)
48 {
49 struct s2 arr[5];
50 arr[i].m_arr[4] = malloc (1024); /* { dg-message "allocated here" } */
51 } /* { dg-warning "leak of 'arr\\\[i\\\].m_arr\\\[4\\\]'" "" { target c } } */
52 /* { dg-warning "leak of 'arr\\\[i\\\].s2::m_arr\\\[4\\\]'" "" { target c++ } .-1 } */
53
54
55 void test_2d (int i, int j)
56 {
57 struct s2 arr[5];
58 arr[i].m_arr[j] = malloc (1024); /* { dg-message "allocated here" } */
59 } /* { dg-warning "leak of 'arr\\\[i\\\].m_arr\\\[j\\\]'" "" { target c } } */
60 /* { dg-warning "leak of 'arr\\\[i\\\].s2::m_arr\\\[j\\\]'" "" { target c++ } .-1 } */
61
62
63 /* Example involving fields. */
64
65 struct s3
66 {
67 struct s3 *m_left;
68 struct s3 *m_right;
69 };
70
71 void test_3 (void)
72 {
73 struct s3 *a = (struct s3 *) malloc (sizeof (struct s3));
74 a->m_right = (struct s3 *) malloc (sizeof (struct s3)); /* { dg-warning "dereference of possibly-NULL 'a'" } */
75 a->m_right->m_left = (struct s3 *) malloc (sizeof (struct s3)); /* { dg-warning "dereference of possibly-NULL '\\*a.m_right'" "" { target c } } */
76 /* { dg-warning "dereference of possibly-NULL '\\*a.s3::m_right'" "" { target c++ } .-1 } */
77 } /* { dg-warning "leak of 'a'" "leak of a" } */
78 /* { dg-warning "leak of '<unknown>'" "leak of unknown" { target *-*-* } .-1 } */
79 /* TODO: rather than '<unknown>', we should print 'a->m_right'
80 and 'a->m_right->m_left' (PR analyzer/99771). */
81
82
83 /* Example involving faking inheritance via casts. */
84
85 struct s4_base
86 {
87 int m_placeholder;
88 };
89
90 struct s4_sub
91 {
92 void *m_buffer;
93 };
94
95 static struct s4_sub *
96 make_s4_sub (void)
97 {
98 struct s4_sub *sub = (struct s4_sub *) malloc (sizeof (struct s4_sub)); /* { dg-message "allocated here" } */
99 if (!sub)
100 return NULL;
101 sub->m_buffer = malloc (1024); /* { dg-message "allocated here" } */
102 return sub;
103 }
104
105 void test_4 (void)
106 {
107 struct s4_base *base = (struct s4_base *)make_s4_sub ();
108 } /* { dg-warning "leak of 'base'" "leak of base" } */
109 /* { dg-warning "leak of '<unknown>'" "leak of sub buffer" { target *-*-* } .-1 } */
110 /* TODO: rather than 'unknown', we should print something
111 like '((struct s4_sub *)base)->m_buffer' (PR analyzer/99771). */