1 // RUN: %clang_analyze_cc1 -verify \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
4 // RUN: -analyzer-checker=alpha.core.CastSize \
5 // RUN: -analyzer-checker=unix.Malloc \
6 // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true %s
8 typedef __typeof(sizeof(int)) size_t;
11 void *realloc(void *ptr
, size_t size
);
12 void *calloc(size_t nmemb
, size_t size
);
13 void __attribute((ownership_returns(malloc
))) *my_malloc(size_t);
14 void __attribute((ownership_takes(malloc
, 1))) my_free(void *);
15 void my_freeBoth(void *, void *)
16 __attribute((ownership_holds(malloc
, 1, 2)));
17 void __attribute((ownership_returns(malloc
, 1))) *my_malloc2(size_t);
18 void __attribute((ownership_holds(malloc
, 1))) my_hold(void *);
20 // Duplicate attributes are silly, but not an error.
21 // Duplicate attribute has no extra effect.
22 // If two are of different kinds, that is an error and reported as such.
23 void __attribute((ownership_holds(malloc
, 1)))
24 __attribute((ownership_holds(malloc
, 1)))
25 __attribute((ownership_holds(malloc
, 3))) my_hold2(void *, void *, void *);
26 void *my_malloc3(size_t);
27 void *myglobalpointer
;
31 struct stuff myglobalstuff
;
35 return; // expected-warning{{Potential leak of memory pointed to by}}
41 free(p
); // expected-warning{{Attempt to free released memory}}
44 void f2_realloc_0(void) {
47 realloc(p
,0); // expected-warning{{Attempt to free released memory}}
50 void f2_realloc_1(void) {
52 int *q
= realloc(p
,0); // no-warning
55 // ownership attributes tests
57 int *p
= my_malloc3(12);
62 int *p
= my_malloc2(12);
63 return; // expected-warning{{Potential leak of memory pointed to by}}
67 int *p
= my_malloc(12);
68 return; // expected-warning{{Potential leak of memory pointed to by}}
72 int *p
= my_malloc(12);
73 } // expected-warning{{Potential leak of memory pointed to by}}
76 myglobalpointer
= my_malloc(12); // no-warning
81 mystuff
.somefield
= my_malloc(12);
82 } // expected-warning{{Potential leak of memory pointed to by}}
84 // Test that we can pass out allocated memory via pointer-to-pointer.
85 void af1_e(void **pp
) {
86 *pp
= my_malloc(42); // no-warning
89 void af1_f(struct stuff
*somestuff
) {
90 somestuff
->somefield
= my_malloc(12); // no-warning
93 // Allocating memory for a field via multiple indirections to our arguments is OK.
94 void af1_g(struct stuff
**pps
) {
95 *pps
= my_malloc(sizeof(struct stuff
)); // no-warning
96 (*pps
)->somefield
= my_malloc(42); // no-warning
100 int *p
= my_malloc(12);
102 free(p
); // expected-warning{{Attempt to free released memory}}
106 int *p
= my_malloc(12);
108 my_free(p
); // expected-warning{{Attempt to free released memory}}
112 int *p
= my_malloc(12);
114 my_hold(p
); // expected-warning{{Attempt to free released memory}}
118 int *p
= my_malloc(12);
120 my_hold2(0, 0, p
); // expected-warning{{Attempt to free released memory}}
123 // No leak if malloc returns null.
125 int *p
= my_malloc(12);
127 return; // no-warning
128 free(p
); // no-warning
131 // This case inflicts a possible double-free.
133 int *p
= my_malloc(12);
135 free(p
); // expected-warning{{Attempt to free non-owned memory}}
139 int *p
= my_malloc(12);
141 return p
; // expected-warning{{Use of memory after it is freed}}
144 // This case is (possibly) ok, be conservative
146 int *p
= my_malloc(12);
148 return p
; // no-warning
153 // This case tests that storing malloc'ed memory to a static variable which is
154 // then returned is not leaked. In the absence of known contracts for functions
155 // or inter-procedural analysis, this is a conservative answer.
159 return p
; // no-warning
162 // This case tests that storing malloc'ed memory to a static global variable
163 // which is then returned is not leaked. In the absence of known contracts for
164 // functions or inter-procedural analysis, this is a conservative answer.
165 static int *p_f4
= 0;
168 return p_f4
; // no-warning
174 return q
; // no-warning
180 return; // no-warning
185 void f6_realloc(void) {
188 return; // no-warning
205 char *x
= (char*) malloc(4);
207 x
[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
210 void f7_realloc(void) {
211 char *x
= (char*) malloc(4);
213 x
[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
217 int *x
= malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
221 int *buf
= malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
222 buf
[1] = 'c'; // not crash
225 void mallocCastToVoid(void) {
227 const void *cp
= p
; // not crash
231 void mallocCastToFP(void) {
233 void (*fp
)(void) = p
; // not crash
237 // This tests that malloc() buffers are undefined by default
238 char mallocGarbage (void) {
239 char *buf
= malloc(2);
240 char result
= buf
[1]; // expected-warning{{undefined}}
245 // This tests that calloc() buffers need to be freed
246 void callocNoFree (void) {
247 char *buf
= calloc(2,2);
248 return; // expected-warning{{Potential leak of memory pointed to by}}
251 // These test that calloc() buffers are zeroed by default
252 char callocZeroesGood (void) {
253 char *buf
= calloc(2,2);
254 char result
= buf
[3]; // no-warning
258 return result
; // no-warning
261 char callocZeroesBad (void) {
262 char *buf
= calloc(2,2);
263 char result
= buf
[3]; // no-warning
265 free(buf
); // expected-warning{{never executed}}
267 return result
; // expected-warning{{Potential leak of memory pointed to by}}
270 void testMultipleFreeAnnotations(void) {