blob: c78cc6c6aae44fac2cb56d4896985e49d6411c9c [file] [log] [blame]
Jordan Rosec20c7272012-09-20 01:55:32 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=unix.Malloc -analyzer-inline-max-stack-depth=5 -verify %s
Anna Zaksa19581a2012-02-20 22:25:23 +00002
Chandler Carruth1b22cec2012-09-12 01:11:10 +00003#include "Inputs/system-header-simulator.h"
Anna Zaksa19581a2012-02-20 22:25:23 +00004
Anna Zaksa19581a2012-02-20 22:25:23 +00005void *malloc(size_t);
6void *valloc(size_t);
7void free(void *);
8void *realloc(void *ptr, size_t size);
9void *reallocf(void *ptr, size_t size);
10void *calloc(size_t nmemb, size_t size);
Jordan Rosec20c7272012-09-20 01:55:32 +000011
12void exit(int) __attribute__ ((__noreturn__));
13void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
14size_t strlen(const char *);
Anna Zaksa19581a2012-02-20 22:25:23 +000015
16static void my_malloc1(void **d, size_t size) {
17 *d = malloc(size);
18}
19
20static void *my_malloc2(int elevel, size_t size) {
21 void *data;
22 data = malloc(size);
23 if (data == 0)
24 exit(0);
25 return data;
26}
27
28static void my_free1(void *p) {
29 free(p);
30}
31
32static void test1() {
33 void *data = 0;
Jordan Rose63bc1862012-11-15 19:11:43 +000034 my_malloc1(&data, 4);
Anna Zaks68eb4c22013-04-06 00:41:36 +000035} // expected-warning {{Potential leak of memory pointed to by 'data'}}
Anna Zaksa19581a2012-02-20 22:25:23 +000036
Anna Zaks36205472012-02-21 00:00:48 +000037static void test11() {
38 void *data = 0;
39 my_malloc1(&data, 4);
40 my_free1(data);
41}
42
Anna Zaks7752d292012-02-27 23:40:55 +000043static void testUniqueingByallocationSiteInTopLevelFunction() {
44 void *data = my_malloc2(1, 4);
45 data = 0;
Anna Zaks68eb4c22013-04-06 00:41:36 +000046 int x = 5;// expected-warning {{Potential leak of memory pointed to by 'data'}}
Jordan Rose63bc1862012-11-15 19:11:43 +000047 data = my_malloc2(1, 4);
Anna Zaks68eb4c22013-04-06 00:41:36 +000048} // expected-warning {{Potential leak of memory pointed to by 'data'}}
Anna Zaksa19581a2012-02-20 22:25:23 +000049
50static void test3() {
51 void *data = my_malloc2(1, 4);
52 free(data);
53 data = my_malloc2(1, 4);
54 free(data);
55}
56
57int test4() {
58 int *data = (int*)my_malloc2(1, 4);
59 my_free1(data);
60 data = (int *)my_malloc2(1, 4);
61 my_free1(data);
62 return *data; // expected-warning {{Use of memory after it is freed}}
63}
64
Anna Zaks36205472012-02-21 00:00:48 +000065void test6() {
66 int *data = (int *)my_malloc2(1, 4);
67 my_free1((int*)data);
68 my_free1((int*)data); // expected-warning{{Use of memory after it is freed}}
69}
70
71// TODO: We should warn here.
72void test5() {
73 int *data;
74 my_free1((int*)data);
75}
Anna Zakse55b03a2012-02-24 16:49:41 +000076
Anna Zaksff80afc2012-02-24 16:49:46 +000077static char *reshape(char *in) {
78 return 0;
79}
80
81void testThatRemoveDeadBindingsRunBeforeEachCall() {
82 char *v = malloc(12);
83 v = reshape(v);
Anna Zaks68eb4c22013-04-06 00:41:36 +000084 v = reshape(v);// expected-warning {{Potential leak of memory pointed to by 'v'}}
Anna Zaksff80afc2012-02-24 16:49:46 +000085}
86
Anna Zakse55b03a2012-02-24 16:49:41 +000087// Test that we keep processing after 'return;'
88void fooWithEmptyReturn(int x) {
89 if (x)
90 return;
91 x++;
92 return;
93}
94
95int uafAndCallsFooWithEmptyReturn() {
96 int *x = (int*)malloc(12);
97 free(x);
98 fooWithEmptyReturn(12);
99 return *x; // expected-warning {{Use of memory after it is freed}}
100}
Jordan Rosec20c7272012-09-20 01:55:32 +0000101
102
103// If we inline any of the malloc-family functions, the checker shouldn't also
104// try to do additional modeling. <rdar://problem/12317671>
105char *strndup(const char *str, size_t n) {
106 if (!str)
107 return 0;
108
109 // DO NOT FIX. This is to test that we are actually using the inlined
110 // behavior!
111 if (n < 5)
112 return 0;
113
114 size_t length = strlen(str);
115 if (length < n)
116 n = length;
117
118 char *result = malloc(n + 1);
119 memcpy(result, str, n);
120 result[n] = '\0';
121 return result;
122}
123
124void useStrndup(size_t n) {
Jordan Rose63bc1862012-11-15 19:11:43 +0000125 if (n == 0) {
Jordan Rosec20c7272012-09-20 01:55:32 +0000126 (void)strndup(0, 20); // no-warning
Jordan Rose63bc1862012-11-15 19:11:43 +0000127 return;
128 } else if (n < 5) {
Jordan Rosec20c7272012-09-20 01:55:32 +0000129 (void)strndup("hi there", n); // no-warning
Jordan Rose63bc1862012-11-15 19:11:43 +0000130 return;
131 } else {
132 (void)strndup("hi there", n);
133 return; // expected-warning{{leak}}
134 }
Jordan Rosec20c7272012-09-20 01:55:32 +0000135}