malloc.c revision f8b1c316cb294d4d47579fbdf7d97d3260e2ba6e
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,experimental.unix.Malloc -analyzer-store=region -verify %s
2typedef __typeof(sizeof(int)) size_t;
3void *malloc(size_t);
4void free(void *);
5void *realloc(void *ptr, size_t size);
6void *calloc(size_t nmemb, size_t size);
7
8void myfoo(int *p);
9void myfooint(int p);
10
11void f1() {
12  int *p = malloc(12);
13  return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
14}
15
16void f2() {
17  int *p = malloc(12);
18  free(p);
19  free(p); // expected-warning{{Try to free a memory block that has been released}}
20}
21
22void f2_realloc_0() {
23  int *p = malloc(12);
24  realloc(p,0);
25  realloc(p,0); // expected-warning{{Try to free a memory block that has been released}}
26}
27
28void f2_realloc_1() {
29  int *p = malloc(12);
30  int *q = realloc(p,0); // no-warning
31}
32
33// This case tests that storing malloc'ed memory to a static variable which is
34// then returned is not leaked.  In the absence of known contracts for functions
35// or inter-procedural analysis, this is a conservative answer.
36int *f3() {
37  static int *p = 0;
38  p = malloc(12);
39  return p; // no-warning
40}
41
42// This case tests that storing malloc'ed memory to a static global variable
43// which is then returned is not leaked.  In the absence of known contracts for
44// functions or inter-procedural analysis, this is a conservative answer.
45static int *p_f4 = 0;
46int *f4() {
47  p_f4 = malloc(12);
48  return p_f4; // no-warning
49}
50
51int *f5() {
52  int *q = malloc(12);
53  q = realloc(q, 20);
54  return q; // no-warning
55}
56
57void f6() {
58  int *p = malloc(12);
59  if (!p)
60    return; // no-warning
61  else
62    free(p);
63}
64
65void f6_realloc() {
66  int *p = malloc(12);
67  if (!p)
68    return; // no-warning
69  else
70    realloc(p,0);
71}
72
73
74char *doit2();
75void pr6069() {
76  char *buf = doit2();
77  free(buf);
78}
79
80void pr6293() {
81  free(0);
82}
83
84void f7() {
85  char *x = (char*) malloc(4);
86  free(x);
87  x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
88}
89
90void f7_realloc() {
91  char *x = (char*) malloc(4);
92  realloc(x,0);
93  x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
94}
95
96void PR6123() {
97  int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
98}
99
100void PR7217() {
101  int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
102  buf[1] = 'c'; // not crash
103}
104
105void mallocCastToVoid() {
106  void *p = malloc(2);
107  const void *cp = p; // not crash
108  free(p);
109}
110
111void mallocCastToFP() {
112  void *p = malloc(2);
113  void (*fp)() = p; // not crash
114  free(p);
115}
116
117// This tests that malloc() buffers are undefined by default
118char mallocGarbage () {
119	char *buf = malloc(2);
120	char result = buf[1]; // expected-warning{{undefined}}
121	free(buf);
122	return result;
123}
124
125// This tests that calloc() buffers need to be freed
126void callocNoFree () {
127  char *buf = calloc(2,2);
128  return; // expected-warning{{never released}}
129}
130
131// These test that calloc() buffers are zeroed by default
132char callocZeroesGood () {
133	char *buf = calloc(2,2);
134	char result = buf[3]; // no-warning
135	if (buf[1] == 0) {
136	  free(buf);
137	}
138	return result; // no-warning
139}
140
141char callocZeroesBad () {
142	char *buf = calloc(2,2);
143	char result = buf[3]; // no-warning
144	if (buf[1] != 0) {
145	  free(buf); // expected-warning{{never executed}}
146	}
147	return result; // expected-warning{{never released}}
148}
149
150void nullFree() {
151  int *p = 0;
152  free(p); // no warning - a nop
153}
154
155void paramFree(int *p) {
156  myfoo(p);
157  free(p); // no warning
158  myfoo(p); // TODO: This should be a warning.
159}
160
161int* mallocEscapeRet() {
162  int *p = malloc(12);
163  return p; // no warning
164}
165
166void mallocEscapeFoo() {
167  int *p = malloc(12);
168  myfoo(p);
169  return; // no warning
170}
171
172void mallocEscapeFree() {
173  int *p = malloc(12);
174  myfoo(p);
175  free(p);
176}
177
178void mallocEscapeFreeFree() {
179  int *p = malloc(12);
180  myfoo(p);
181  free(p);
182  free(p); // expected-warning{{Try to free a memory block that has been released}}
183}
184
185void mallocEscapeFreeUse() {
186  int *p = malloc(12);
187  myfoo(p);
188  free(p);
189  myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
190}
191
192int *myalloc();
193void myalloc2(int **p);
194
195void mallocEscapeFreeCustomAlloc() {
196  int *p = malloc(12);
197  myfoo(p);
198  free(p);
199  p = myalloc();
200  free(p); // no warning
201}
202
203void mallocEscapeFreeCustomAlloc2() {
204  int *p = malloc(12);
205  myfoo(p);
206  free(p);
207  myalloc2(&p);
208  free(p); // no warning
209}
210
211void mallocBindFreeUse() {
212  int *x = malloc(12);
213  int *y = x;
214  free(y);
215  myfoo(x); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
216}
217
218void mallocEscapeMalloc() {
219  int *p = malloc(12);
220  myfoo(p);
221  p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}}
222}
223
224void mallocMalloc() {
225  int *p = malloc(12);
226  p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}}
227}
228
229void mallocFreeMalloc() {
230  int *p = malloc(12);
231  free(p);
232  p = malloc(12);
233  free(p);
234}
235
236void mallocFreeUse_params() {
237  int *p = malloc(12);
238  free(p);
239  myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
240  myfooint(*p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
241}
242
243void mallocFailedOrNot() {
244  int *p = malloc(12);
245  if (!p)
246    free(p);
247  else
248    free(p);
249}
250
251struct StructWithInt {
252  int g;
253};
254void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
255
256void mallocEscapeFooNonSymbolArg() {
257  struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
258  nonSymbolAsFirstArg(&p->g, p);
259  return; // no warning
260}
261
262
263int *Gl;
264struct GlStTy {
265  int *x;
266};
267
268struct GlStTy GlS = {0};
269
270void GlobalFree() {
271  free(Gl);
272}
273
274void GlobalMalloc() {
275  Gl = malloc(12);
276}
277
278void GlobalStructMalloc() {
279  int *a = malloc(12);
280  GlS.x = a;
281}
282
283void GlobalStructMallocFree() {
284  int *a = malloc(12);
285  GlS.x = a;
286  free(GlS.x);
287}
288
289
290// Below are the known false positives.
291
292// TODO: There should be no warning here.
293extern void exit(int) __attribute__ ((__noreturn__));
294void mallocExit(int *g) {
295  struct xx *p = malloc(12);
296
297  if (g != 0) {
298    exit(1); // expected-warning{{Allocated memory never released. Potential memory leak}}
299  }
300  free(p);
301  return;
302}
303
304
305// TODO: There should be no warning here.
306extern void __assert_fail (__const char *__assertion, __const char *__file,
307    unsigned int __line, __const char *__function)
308     __attribute__ ((__noreturn__));
309#define assert(expr) \
310  ((expr)  ? (void)(0)  : __assert_fail (#expr, __FILE__, __LINE__, __func__))
311void mallocAssert(int *g) {
312  struct xx *p = malloc(12);
313
314  assert(g != 0); // expected-warning{{Allocated memory never released. Potential memory leak}}
315  free(p);
316  return;
317}
318
319// TODO: There should be no warning here.
320unsigned takePtrToPtr(int **p);
321void PassTheAddrOfAllocatedData(int *g, int f) {
322  int *p = malloc(12);
323  // This call is causing the problem.
324  if (takePtrToPtr(&p))
325    f++; // expected-warning{{Allocated memory never released. Potential memory leak}}
326  free(p); // expected-warning{{Allocated memory never released. Potential memory leak}}
327}
328
329// TODO: There should be no warning here.
330void reallocFails(int *g, int f) {
331  char *p = malloc(12);
332  char *r = realloc(p, 12+1);
333  if (!r) {
334    free(p); // expected-warning {{Try to free a memory block that has been released}}
335  } else {
336    free(r);
337  }
338}
339
340// TODO: There should be no warning here. This one might be difficult to get rid of.
341void dependsOnValueOfPtr(int *g, unsigned f) {
342  int *p;
343
344  if (f) {
345    p = g;
346  } else {
347    p = malloc(12);
348  }
349
350  if (p != g)
351    free(p);
352  else
353    return; // expected-warning{{Allocated memory never released. Potential memory leak}}
354  return;
355}
356
357// TODO: Should this be a warning?
358// Here we are returning a pointer one past the allocated value. An idiom which
359// can be used for implementing special malloc. The correct uses of this might
360// be rare enough so that we could keep this as a warning.
361static void *specialMalloc(int n){
362  int *p;
363  p = malloc( n+8 );
364  if( p ){
365    p[0] = n;
366    p++;
367  }
368  return p;// expected-warning {{Allocated memory never released. Potential memory leak.}}
369}
370