Debian Codegraph @

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

raw
1 /* { dg-additional-options "-Wno-free-nonheap-object" } */
2
3 typedef __SIZE_TYPE__ size_t;
4
5 #include "../../gcc.dg/analyzer/analyzer-decls.h"
6
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__));
17
18 void *test_1 (void *ptr)
19 {
20 return realloc (ptr, 1024);
21 }
22
23 void test_2 (void *ptr)
24 {
25 void *p = malloc (1024); /* { dg-message "allocated here" } */
26 p = realloc (p, 4096); /* { dg-message "when 'realloc' fails" } */
27 free (p);
28 } /* { dg-warning "leak of 'p'" } */ // ideally this would be on the realloc stmt
29
30 void test_3 (void *ptr)
31 {
32 void *p = malloc (1024);
33 void *q = realloc (p, 4096);
34 if (q)
35 free (q);
36 else
37 free (p);
38 }
39
40 void *test_4 (void)
41 {
42 return realloc (NULL, 1024);
43 }
44
45 int *test_5 (int *p)
46 {
47 *p = 42;
48 int *q = (int *) realloc (p, sizeof(int) * 4); /* { dg-message "when 'realloc' fails" } */
49 *q = 43; /* { dg-warning "dereference of NULL 'q'" } */
50 return q;
51 }
52
53 void test_6 (size_t sz)
54 {
55 void *p = realloc (NULL, sz);
56 } /* { dg-warning "leak of 'p'" } */
57
58 /* The analyzer should complain about realloc of non-heap. */
59
60 void *test_7 (size_t sz)
61 {
62 char buf[100]; /* { dg-message "region created on stack here" } */
63 void *p = realloc (&buf, sz); /* { dg-warning "'realloc' of '& ?buf' which points to memory on the stack" } */
64 return p;
65 }
66
67 /* Mismatched allocator. */
68
69 struct foo
70 {
71 int m_int;
72 };
73
74 extern void foo_release (struct foo *);
75 extern struct foo *foo_acquire (void)
76 __attribute__ ((malloc (foo_release)));
77
78 void test_8 (void)
79 {
80 struct foo *p = foo_acquire ();
81 void *q = realloc (p, 1024); /* { dg-warning "'p' should have been deallocated with 'foo_release' but was deallocated with 'realloc'" } */
82 }
83
84 /* We should complain about realloc on a freed pointer. */
85
86 void test_9 (void *p)
87 {
88 free (p);
89 void *q = realloc (p, 1024); /* { dg-warning "double-'free' of 'p'" } */
90 }
91
92 void test_10 (char *s, int n)
93 {
94 __builtin_realloc(s, n); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" "" { target c } } */
95 /* { dg-warning "ignoring return value of 'void\\* __builtin_realloc\\(void\\*, (long )*unsigned int\\)' declared with attribute 'warn_unused_result'" "" { target c++ } .-1 } */
96 } /* { dg-warning "leak" } */