1 // RUN: %clang_cc1 -analyze -analyzer-checker=unix.Malloc -analyzer-inline-call -analyzer-store=region -verify %s
2 
3 #include "system-header-simulator.h"
4 
5 typedef __typeof(sizeof(int)) size_t;
6 void *malloc(size_t);
7 void *valloc(size_t);
8 void free(void *);
9 void *realloc(void *ptr, size_t size);
10 void *reallocf(void *ptr, size_t size);
11 void *calloc(size_t nmemb, size_t size);
12 extern void exit(int) __attribute__ ((__noreturn__));
13 
14 static void my_malloc1(void **d, size_t size) {
15   *d = malloc(size);
16 }
17 
18 static void *my_malloc2(int elevel, size_t size) {
19   void     *data;
20   data = malloc(size);
21   if (data == 0)
22     exit(0);
23   return data;
24 }
25 
26 static void my_free1(void *p) {
27   free(p);
28 }
29 
30 static void test1() {
31   void *data = 0;
32   my_malloc1(&data, 4); // expected-warning {{Memory is never released; potential memory leak}}
33 }
34 
35 static void test11() {
36   void *data = 0;
37   my_malloc1(&data, 4);
38   my_free1(data);
39 }
40 
41 static void test2() {
42   void * data = my_malloc2(1, 4);
43   data = my_malloc2(1, 4);// expected-warning {{Memory is never released; potential memory leak}}
44 }
45 
46 static void test3() {
47   void *data = my_malloc2(1, 4);
48   free(data);
49   data = my_malloc2(1, 4);
50   free(data);
51 }
52 
53 int test4() {
54   int *data = (int*)my_malloc2(1, 4);
55   my_free1(data);
56   data = (int *)my_malloc2(1, 4);
57   my_free1(data);
58   return *data; // expected-warning {{Use of memory after it is freed}}
59 }
60 
61 void test6() {
62   int *data = (int *)my_malloc2(1, 4);
63   my_free1((int*)data);
64   my_free1((int*)data); // expected-warning{{Use of memory after it is freed}}
65 }
66 
67 // TODO: We should warn here.
68 void test5() {
69   int *data;
70   my_free1((int*)data);
71 }
72