Ted Kremenek | cdc3a89 | 2012-08-24 20:39:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,debug.ExprInspection -analyzer-store=region -verify %s |
NAKAMURA Takumi | 7a29070 | 2012-11-19 10:00:59 +0000 | [diff] [blame] | 2 | |
Chandler Carruth | 1b22cec | 2012-09-12 01:11:10 +0000 | [diff] [blame] | 3 | #include "Inputs/system-header-simulator.h" |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 4 | |
Anna Zaks | adccc3f | 2012-06-08 00:04:40 +0000 | [diff] [blame] | 5 | void clang_analyzer_eval(int); |
| 6 | |
Eli Friedman | 2f00552 | 2009-11-14 04:23:25 +0000 | [diff] [blame] | 7 | typedef __typeof(sizeof(int)) size_t; |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 8 | void *malloc(size_t); |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 9 | void *valloc(size_t); |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 10 | void free(void *); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 11 | void *realloc(void *ptr, size_t size); |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 12 | void *reallocf(void *ptr, size_t size); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 13 | void *calloc(size_t nmemb, size_t size); |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 14 | char *strdup(const char *s); |
| 15 | char *strndup(const char *s, size_t n); |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 16 | int memcmp(const void *s1, const void *s2, size_t n); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 17 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 18 | void myfoo(int *p); |
| 19 | void myfooint(int p); |
Anna Zaks | ebc1d32 | 2012-02-15 00:11:28 +0000 | [diff] [blame] | 20 | char *fooRetPtr(); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 21 | |
| 22 | void f1() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 23 | int *p = malloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 24 | return; // expected-warning{{Potential leak of memory pointed to by 'p'}} |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 27 | void f2() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 28 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 29 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 30 | free(p); // expected-warning{{Attempt to free released memory}} |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 31 | } |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 32 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 33 | void f2_realloc_0() { |
| 34 | int *p = malloc(12); |
| 35 | realloc(p,0); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 36 | realloc(p,0); // expected-warning{{Attempt to free released memory}} |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void f2_realloc_1() { |
| 40 | int *p = malloc(12); |
Zhongxing Xu | d56763f | 2011-09-01 04:53:59 +0000 | [diff] [blame] | 41 | int *q = realloc(p,0); // no-warning |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 44 | void reallocNotNullPtr(unsigned sizeIn) { |
| 45 | unsigned size = 12; |
| 46 | char *p = (char*)malloc(size); |
| 47 | if (p) { |
| 48 | char *q = (char*)realloc(p, sizeIn); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 49 | char x = *q; // expected-warning {{Potential leak of memory pointed to by 'q'}} |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | int *realloctest1() { |
| 54 | int *q = malloc(12); |
| 55 | q = realloc(q, 20); |
| 56 | return q; // no warning - returning the allocated value |
| 57 | } |
| 58 | |
| 59 | // p should be freed if realloc fails. |
| 60 | void reallocFails() { |
| 61 | char *p = malloc(12); |
| 62 | char *r = realloc(p, 12+1); |
| 63 | if (!r) { |
| 64 | free(p); |
| 65 | } else { |
| 66 | free(r); |
| 67 | } |
| 68 | } |
| 69 | |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 70 | void reallocSizeZero1() { |
| 71 | char *p = malloc(12); |
| 72 | char *r = realloc(p, 0); |
| 73 | if (!r) { |
Anna Zaks | ede875b | 2012-08-03 18:30:18 +0000 | [diff] [blame] | 74 | free(p); // expected-warning {{Attempt to free released memory}} |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 75 | } else { |
| 76 | free(r); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void reallocSizeZero2() { |
| 81 | char *p = malloc(12); |
| 82 | char *r = realloc(p, 0); |
| 83 | if (!r) { |
Anna Zaks | ede875b | 2012-08-03 18:30:18 +0000 | [diff] [blame] | 84 | free(p); // expected-warning {{Attempt to free released memory}} |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 85 | } else { |
| 86 | free(r); |
| 87 | } |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 88 | free(p); // expected-warning {{Attempt to free released memory}} |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void reallocSizeZero3() { |
| 92 | char *p = malloc(12); |
| 93 | char *r = realloc(p, 0); |
| 94 | free(r); |
| 95 | } |
| 96 | |
| 97 | void reallocSizeZero4() { |
| 98 | char *r = realloc(0, 0); |
| 99 | free(r); |
| 100 | } |
| 101 | |
| 102 | void reallocSizeZero5() { |
| 103 | char *r = realloc(0, 0); |
| 104 | } |
| 105 | |
| 106 | void reallocPtrZero1() { |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 107 | char *r = realloc(0, 12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 108 | } // expected-warning {{Potential leak of memory pointed to by 'r'}} |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 109 | |
| 110 | void reallocPtrZero2() { |
| 111 | char *r = realloc(0, 12); |
| 112 | if (r) |
| 113 | free(r); |
| 114 | } |
| 115 | |
| 116 | void reallocPtrZero3() { |
| 117 | char *r = realloc(0, 12); |
| 118 | free(r); |
| 119 | } |
| 120 | |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 121 | void reallocRadar6337483_1() { |
| 122 | char *buf = malloc(100); |
| 123 | buf = (char*)realloc(buf, 0x1000000); |
| 124 | if (!buf) { |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 125 | return;// expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 126 | } |
| 127 | free(buf); |
| 128 | } |
| 129 | |
| 130 | void reallocRadar6337483_2() { |
| 131 | char *buf = malloc(100); |
| 132 | char *buf2 = (char*)realloc(buf, 0x1000000); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 133 | if (!buf2) { |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 134 | ; |
| 135 | } else { |
| 136 | free(buf2); |
| 137 | } |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 138 | } // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 139 | |
| 140 | void reallocRadar6337483_3() { |
| 141 | char * buf = malloc(100); |
| 142 | char * tmp; |
| 143 | tmp = (char*)realloc(buf, 0x1000000); |
| 144 | if (!tmp) { |
| 145 | free(buf); |
| 146 | return; |
| 147 | } |
| 148 | buf = tmp; |
| 149 | free(buf); |
| 150 | } |
| 151 | |
| 152 | void reallocRadar6337483_4() { |
| 153 | char *buf = malloc(100); |
| 154 | char *buf2 = (char*)realloc(buf, 0x1000000); |
| 155 | if (!buf2) { |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 156 | return; // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 157 | } else { |
| 158 | free(buf2); |
| 159 | } |
| 160 | } |
| 161 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 162 | int *reallocfTest1() { |
| 163 | int *q = malloc(12); |
| 164 | q = reallocf(q, 20); |
| 165 | return q; // no warning - returning the allocated value |
| 166 | } |
| 167 | |
| 168 | void reallocfRadar6337483_4() { |
| 169 | char *buf = malloc(100); |
| 170 | char *buf2 = (char*)reallocf(buf, 0x1000000); |
| 171 | if (!buf2) { |
| 172 | return; // no warning - reallocf frees even on failure |
| 173 | } else { |
| 174 | free(buf2); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void reallocfRadar6337483_3() { |
| 179 | char * buf = malloc(100); |
| 180 | char * tmp; |
| 181 | tmp = (char*)reallocf(buf, 0x1000000); |
| 182 | if (!tmp) { |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 183 | free(buf); // expected-warning {{Attempt to free released memory}} |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | buf = tmp; |
| 187 | free(buf); |
| 188 | } |
| 189 | |
| 190 | void reallocfPtrZero1() { |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 191 | char *r = reallocf(0, 12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 192 | } // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 193 | |
| 194 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 195 | // This case tests that storing malloc'ed memory to a static variable which is |
| 196 | // then returned is not leaked. In the absence of known contracts for functions |
| 197 | // or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 198 | int *f3() { |
| 199 | static int *p = 0; |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 200 | p = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 201 | return p; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 204 | // This case tests that storing malloc'ed memory to a static global variable |
| 205 | // which is then returned is not leaked. In the absence of known contracts for |
| 206 | // functions or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 207 | static int *p_f4 = 0; |
| 208 | int *f4() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 209 | p_f4 = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 210 | return p_f4; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 211 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 212 | |
| 213 | int *f5() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 214 | int *q = malloc(12); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 215 | q = realloc(q, 20); |
| 216 | return q; // no-warning |
| 217 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 218 | |
| 219 | void f6() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 220 | int *p = malloc(12); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 221 | if (!p) |
| 222 | return; // no-warning |
| 223 | else |
| 224 | free(p); |
| 225 | } |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 226 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 227 | void f6_realloc() { |
| 228 | int *p = malloc(12); |
| 229 | if (!p) |
| 230 | return; // no-warning |
| 231 | else |
| 232 | realloc(p,0); |
| 233 | } |
| 234 | |
| 235 | |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 236 | char *doit2(); |
| 237 | void pr6069() { |
| 238 | char *buf = doit2(); |
| 239 | free(buf); |
| 240 | } |
Zhongxing Xu | 181cc3d | 2010-02-14 06:49:48 +0000 | [diff] [blame] | 241 | |
| 242 | void pr6293() { |
| 243 | free(0); |
| 244 | } |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 245 | |
| 246 | void f7() { |
| 247 | char *x = (char*) malloc(4); |
| 248 | free(x); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 249 | x[0] = 'a'; // expected-warning{{Use of memory after it is freed}} |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 250 | } |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 251 | |
Anna Zaks | 1434518 | 2012-05-18 01:16:10 +0000 | [diff] [blame] | 252 | void f8() { |
| 253 | char *x = (char*) malloc(4); |
| 254 | free(x); |
| 255 | char *y = strndup(x, 4); // expected-warning{{Use of memory after it is freed}} |
| 256 | } |
| 257 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 258 | void f7_realloc() { |
| 259 | char *x = (char*) malloc(4); |
| 260 | realloc(x,0); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 261 | x[0] = 'a'; // expected-warning{{Use of memory after it is freed}} |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 264 | void PR6123() { |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 265 | int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void PR7217() { |
Ted Kremenek | c4bac8e | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 269 | int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}} |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 270 | buf[1] = 'c'; // not crash |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 271 | } |
Jordy Rose | c580f2e | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 272 | |
| 273 | void mallocCastToVoid() { |
| 274 | void *p = malloc(2); |
| 275 | const void *cp = p; // not crash |
| 276 | free(p); |
| 277 | } |
| 278 | |
| 279 | void mallocCastToFP() { |
| 280 | void *p = malloc(2); |
| 281 | void (*fp)() = p; // not crash |
| 282 | free(p); |
| 283 | } |
| 284 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 285 | // This tests that malloc() buffers are undefined by default |
| 286 | char mallocGarbage () { |
| 287 | char *buf = malloc(2); |
| 288 | char result = buf[1]; // expected-warning{{undefined}} |
| 289 | free(buf); |
| 290 | return result; |
| 291 | } |
| 292 | |
| 293 | // This tests that calloc() buffers need to be freed |
| 294 | void callocNoFree () { |
| 295 | char *buf = calloc(2,2); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 296 | return; // expected-warning{{Potential leak of memory pointed to by 'buf'}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // These test that calloc() buffers are zeroed by default |
| 300 | char callocZeroesGood () { |
| 301 | char *buf = calloc(2,2); |
| 302 | char result = buf[3]; // no-warning |
| 303 | if (buf[1] == 0) { |
| 304 | free(buf); |
| 305 | } |
| 306 | return result; // no-warning |
| 307 | } |
| 308 | |
| 309 | char callocZeroesBad () { |
| 310 | char *buf = calloc(2,2); |
| 311 | char result = buf[3]; // no-warning |
| 312 | if (buf[1] != 0) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 313 | free(buf); // expected-warning{{never executed}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 314 | } |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 315 | return result; // expected-warning{{Potential leak of memory pointed to by 'buf'}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 316 | } |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 317 | |
| 318 | void nullFree() { |
| 319 | int *p = 0; |
| 320 | free(p); // no warning - a nop |
| 321 | } |
| 322 | |
| 323 | void paramFree(int *p) { |
| 324 | myfoo(p); |
| 325 | free(p); // no warning |
Anna Zaks | ede875b | 2012-08-03 18:30:18 +0000 | [diff] [blame] | 326 | myfoo(p); // expected-warning {{Use of memory after it is freed}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | int* mallocEscapeRet() { |
| 330 | int *p = malloc(12); |
| 331 | return p; // no warning |
| 332 | } |
| 333 | |
| 334 | void mallocEscapeFoo() { |
| 335 | int *p = malloc(12); |
| 336 | myfoo(p); |
| 337 | return; // no warning |
| 338 | } |
| 339 | |
| 340 | void mallocEscapeFree() { |
| 341 | int *p = malloc(12); |
| 342 | myfoo(p); |
| 343 | free(p); |
| 344 | } |
| 345 | |
| 346 | void mallocEscapeFreeFree() { |
| 347 | int *p = malloc(12); |
| 348 | myfoo(p); |
| 349 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 350 | free(p); // expected-warning{{Attempt to free released memory}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void mallocEscapeFreeUse() { |
| 354 | int *p = malloc(12); |
| 355 | myfoo(p); |
| 356 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 357 | myfoo(p); // expected-warning{{Use of memory after it is freed}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | int *myalloc(); |
| 361 | void myalloc2(int **p); |
| 362 | |
| 363 | void mallocEscapeFreeCustomAlloc() { |
| 364 | int *p = malloc(12); |
| 365 | myfoo(p); |
| 366 | free(p); |
| 367 | p = myalloc(); |
| 368 | free(p); // no warning |
| 369 | } |
| 370 | |
| 371 | void mallocEscapeFreeCustomAlloc2() { |
| 372 | int *p = malloc(12); |
| 373 | myfoo(p); |
| 374 | free(p); |
| 375 | myalloc2(&p); |
| 376 | free(p); // no warning |
| 377 | } |
| 378 | |
| 379 | void mallocBindFreeUse() { |
| 380 | int *x = malloc(12); |
| 381 | int *y = x; |
| 382 | free(y); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 383 | myfoo(x); // expected-warning{{Use of memory after it is freed}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void mallocEscapeMalloc() { |
| 387 | int *p = malloc(12); |
| 388 | myfoo(p); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 389 | p = malloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 390 | } // expected-warning{{Potential leak of memory pointed to by}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 391 | |
| 392 | void mallocMalloc() { |
| 393 | int *p = malloc(12); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 394 | p = malloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 395 | } // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 396 | |
| 397 | void mallocFreeMalloc() { |
| 398 | int *p = malloc(12); |
| 399 | free(p); |
| 400 | p = malloc(12); |
| 401 | free(p); |
| 402 | } |
| 403 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 404 | void mallocFreeUse_params() { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 405 | int *p = malloc(12); |
| 406 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 407 | myfoo(p); //expected-warning{{Use of memory after it is freed}} |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void mallocFreeUse_params2() { |
| 411 | int *p = malloc(12); |
| 412 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 413 | myfooint(*p); //expected-warning{{Use of memory after it is freed}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 416 | void mallocFailedOrNot() { |
| 417 | int *p = malloc(12); |
| 418 | if (!p) |
| 419 | free(p); |
| 420 | else |
| 421 | free(p); |
| 422 | } |
| 423 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 424 | struct StructWithInt { |
| 425 | int g; |
| 426 | }; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 427 | |
| 428 | int *mallocReturnFreed() { |
| 429 | int *p = malloc(12); |
| 430 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 431 | return p; // expected-warning {{Use of memory after it is freed}} |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | int useAfterFreeStruct() { |
| 435 | struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 436 | px->g = 5; |
| 437 | free(px); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 438 | return px->g; // expected-warning {{Use of memory after it is freed}} |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 441 | void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p); |
| 442 | |
| 443 | void mallocEscapeFooNonSymbolArg() { |
| 444 | struct StructWithInt *p = malloc(sizeof(struct StructWithInt)); |
| 445 | nonSymbolAsFirstArg(&p->g, p); |
| 446 | return; // no warning |
| 447 | } |
| 448 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 449 | void mallocFailedOrNotLeak() { |
| 450 | int *p = malloc(12); |
| 451 | if (p == 0) |
| 452 | return; // no warning |
| 453 | else |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 454 | return; // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 455 | } |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 456 | |
Anna Zaks | ebc1d32 | 2012-02-15 00:11:28 +0000 | [diff] [blame] | 457 | void mallocAssignment() { |
| 458 | char *p = malloc(12); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 459 | p = fooRetPtr(); |
| 460 | } // expected-warning {{leak}} |
Anna Zaks | ebc1d32 | 2012-02-15 00:11:28 +0000 | [diff] [blame] | 461 | |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 462 | int vallocTest() { |
| 463 | char *mem = valloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 464 | return 0; // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void vallocEscapeFreeUse() { |
| 468 | int *p = valloc(12); |
| 469 | myfoo(p); |
| 470 | free(p); |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 471 | myfoo(p); // expected-warning{{Use of memory after it is freed}} |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 474 | int *Gl; |
| 475 | struct GlStTy { |
| 476 | int *x; |
| 477 | }; |
| 478 | |
| 479 | struct GlStTy GlS = {0}; |
| 480 | |
| 481 | void GlobalFree() { |
| 482 | free(Gl); |
| 483 | } |
| 484 | |
| 485 | void GlobalMalloc() { |
| 486 | Gl = malloc(12); |
| 487 | } |
| 488 | |
| 489 | void GlobalStructMalloc() { |
| 490 | int *a = malloc(12); |
| 491 | GlS.x = a; |
| 492 | } |
| 493 | |
| 494 | void GlobalStructMallocFree() { |
| 495 | int *a = malloc(12); |
| 496 | GlS.x = a; |
| 497 | free(GlS.x); |
| 498 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 499 | |
Anna Zaks | ad901a6 | 2012-02-16 22:26:15 +0000 | [diff] [blame] | 500 | char *ArrayG[12]; |
| 501 | |
| 502 | void globalArrayTest() { |
| 503 | char *p = (char*)malloc(12); |
| 504 | ArrayG[0] = p; |
| 505 | } |
| 506 | |
Anna Zaks | ac59300 | 2012-02-16 03:40:57 +0000 | [diff] [blame] | 507 | // Make sure that we properly handle a pointer stored into a local struct/array. |
| 508 | typedef struct _StructWithPtr { |
| 509 | int *memP; |
| 510 | } StructWithPtr; |
| 511 | |
| 512 | static StructWithPtr arrOfStructs[10]; |
| 513 | |
| 514 | void testMalloc() { |
| 515 | int *x = malloc(12); |
| 516 | StructWithPtr St; |
| 517 | St.memP = x; |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 518 | arrOfStructs[0] = St; // no-warning |
Anna Zaks | ac59300 | 2012-02-16 03:40:57 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | StructWithPtr testMalloc2() { |
| 522 | int *x = malloc(12); |
| 523 | StructWithPtr St; |
| 524 | St.memP = x; |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 525 | return St; // no-warning |
Anna Zaks | ac59300 | 2012-02-16 03:40:57 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | int *testMalloc3() { |
| 529 | int *x = malloc(12); |
| 530 | int *y = x; |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 531 | return y; // no-warning |
Anna Zaks | ac59300 | 2012-02-16 03:40:57 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Jordan Rose | 74f6982 | 2013-03-20 20:35:57 +0000 | [diff] [blame] | 534 | void testStructLeak() { |
| 535 | StructWithPtr St; |
| 536 | St.memP = malloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 537 | return; // expected-warning {{Potential leak of memory pointed to by 'St.memP'}} |
Jordan Rose | 74f6982 | 2013-03-20 20:35:57 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Anna Zaks | d8a8a3b | 2012-02-17 22:35:34 +0000 | [diff] [blame] | 540 | void testElemRegion1() { |
| 541 | char *x = (void*)malloc(2); |
| 542 | int *ix = (int*)x; |
| 543 | free(&(x[0])); |
| 544 | } |
| 545 | |
| 546 | void testElemRegion2(int **pp) { |
| 547 | int *p = malloc(12); |
| 548 | *pp = p; |
| 549 | free(pp[0]); |
| 550 | } |
| 551 | |
| 552 | void testElemRegion3(int **pp) { |
| 553 | int *p = malloc(12); |
| 554 | *pp = p; |
| 555 | free(*pp); |
| 556 | } |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 557 | // Region escape testing. |
| 558 | |
| 559 | unsigned takePtrToPtr(int **p); |
| 560 | void PassTheAddrOfAllocatedData(int f) { |
| 561 | int *p = malloc(12); |
| 562 | // We don't know what happens after the call. Should stop tracking here. |
| 563 | if (takePtrToPtr(&p)) |
| 564 | f++; |
| 565 | free(p); // no warning |
| 566 | } |
| 567 | |
| 568 | struct X { |
| 569 | int *p; |
| 570 | }; |
| 571 | unsigned takePtrToStruct(struct X *s); |
| 572 | int ** foo2(int *g, int f) { |
| 573 | int *p = malloc(12); |
| 574 | struct X *px= malloc(sizeof(struct X)); |
| 575 | px->p = p; |
| 576 | // We don't know what happens after this call. Should not track px nor p. |
| 577 | if (takePtrToStruct(px)) |
| 578 | f++; |
| 579 | free(p); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | struct X* RegInvalidationDetect1(struct X *s2) { |
| 584 | struct X *px= malloc(sizeof(struct X)); |
| 585 | px->p = 0; |
| 586 | px = s2; |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 587 | return px; // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | struct X* RegInvalidationGiveUp1() { |
| 591 | int *p = malloc(12); |
| 592 | struct X *px= malloc(sizeof(struct X)); |
| 593 | px->p = p; |
| 594 | return px; |
| 595 | } |
| 596 | |
| 597 | int **RegInvalidationDetect2(int **pp) { |
| 598 | int *p = malloc(12); |
| 599 | pp = &p; |
| 600 | pp++; |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 601 | return 0;// expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 602 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 603 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 604 | extern void exit(int) __attribute__ ((__noreturn__)); |
| 605 | void mallocExit(int *g) { |
| 606 | struct xx *p = malloc(12); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 607 | if (g != 0) |
| 608 | exit(1); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 609 | free(p); |
| 610 | return; |
| 611 | } |
| 612 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 613 | extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 614 | unsigned int __line, __const char *__function) |
| 615 | __attribute__ ((__noreturn__)); |
| 616 | #define assert(expr) \ |
| 617 | ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
| 618 | void mallocAssert(int *g) { |
| 619 | struct xx *p = malloc(12); |
| 620 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 621 | assert(g != 0); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 622 | free(p); |
| 623 | return; |
| 624 | } |
| 625 | |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 626 | void doNotInvalidateWhenPassedToSystemCalls(char *s) { |
| 627 | char *p = malloc(12); |
| 628 | strlen(p); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 629 | strcpy(p, s); |
| 630 | } // expected-warning {{leak}} |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 631 | |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 632 | // Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p. |
| 633 | void symbolLostWithStrcpy(char *s) { |
| 634 | char *p = malloc(12); |
| 635 | p = strcpy(p, s); |
| 636 | free(p); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | // The same test as the one above, but with what is actually generated on a mac. |
| 641 | static __inline char * |
| 642 | __inline_strcpy_chk (char *restrict __dest, const char *restrict __src) |
| 643 | { |
| 644 | return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1)); |
| 645 | } |
| 646 | |
| 647 | void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) { |
| 648 | char *p = malloc(12); |
| 649 | p = ((__builtin_object_size (p, 0) != (size_t) -1) ? __builtin___strcpy_chk (p, s, __builtin_object_size (p, 2 > 1)) : __inline_strcpy_chk (p, s)); |
| 650 | free(p); |
| 651 | } |
Anna Zaks | d9ab7bb | 2012-02-22 02:36:01 +0000 | [diff] [blame] | 652 | |
| 653 | // Here we are returning a pointer one past the allocated value. An idiom which |
| 654 | // can be used for implementing special malloc. The correct uses of this might |
| 655 | // be rare enough so that we could keep this as a warning. |
| 656 | static void *specialMalloc(int n){ |
| 657 | int *p; |
| 658 | p = malloc( n+8 ); |
| 659 | if( p ){ |
| 660 | p[0] = n; |
| 661 | p++; |
| 662 | } |
| 663 | return p; |
| 664 | } |
| 665 | |
| 666 | // Potentially, the user could free the struct by performing pointer arithmetic on the return value. |
| 667 | // This is a variation of the specialMalloc issue, though probably would be more rare in correct code. |
| 668 | int *specialMallocWithStruct() { |
| 669 | struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 670 | return &(px->g); |
| 671 | } |
| 672 | |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 673 | // Test various allocation/deallocation functions. |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 674 | void testStrdup(const char *s, unsigned validIndex) { |
| 675 | char *s2 = strdup(s); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 676 | s2[validIndex + 1] = 'b'; |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 677 | } // expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 678 | |
| 679 | int testStrndup(const char *s, unsigned validIndex, unsigned size) { |
| 680 | char *s2 = strndup(s, size); |
| 681 | s2 [validIndex + 1] = 'b'; |
| 682 | if (s2[validIndex] != 'a') |
Anna Zaks | ca8e36e | 2012-02-23 21:38:21 +0000 | [diff] [blame] | 683 | return 0; |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 684 | else |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 685 | return 1;// expected-warning {{Potential leak of memory pointed to by}} |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Anna Zaks | 87cb5be | 2012-02-22 19:24:52 +0000 | [diff] [blame] | 688 | void testStrdupContentIsDefined(const char *s, unsigned validIndex) { |
| 689 | char *s2 = strdup(s); |
| 690 | char result = s2[1];// no warning |
| 691 | free(s2); |
| 692 | } |
| 693 | |
Anna Zaks | ca23eb2 | 2012-02-29 18:42:47 +0000 | [diff] [blame] | 694 | // ---------------------------------------------------------------------------- |
Anna Zaks | 0d389b8 | 2012-02-23 01:05:27 +0000 | [diff] [blame] | 695 | // Test the system library functions to which the pointer can escape. |
Anna Zaks | ca23eb2 | 2012-02-29 18:42:47 +0000 | [diff] [blame] | 696 | // This tests false positive suppression. |
Anna Zaks | 0d389b8 | 2012-02-23 01:05:27 +0000 | [diff] [blame] | 697 | |
| 698 | // For now, we assume memory passed to pthread_specific escapes. |
| 699 | // TODO: We could check that if a new pthread binding is set, the existing |
| 700 | // binding must be freed; otherwise, a memory leak can occur. |
| 701 | void testPthereadSpecificEscape(pthread_key_t key) { |
| 702 | void *buf = malloc(12); |
| 703 | pthread_setspecific(key, buf); // no warning |
| 704 | } |
| 705 | |
Anna Zaks | ca23eb2 | 2012-02-29 18:42:47 +0000 | [diff] [blame] | 706 | // PR12101: Test funopen(). |
| 707 | static int releasePtr(void *_ctx) { |
| 708 | free(_ctx); |
| 709 | return 0; |
| 710 | } |
| 711 | FILE *useFunOpen() { |
| 712 | void *ctx = malloc(sizeof(int)); |
| 713 | FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning |
| 714 | if (f == 0) { |
| 715 | free(ctx); |
| 716 | } |
| 717 | return f; |
| 718 | } |
| 719 | FILE *useFunOpenNoReleaseFunction() { |
| 720 | void *ctx = malloc(sizeof(int)); |
| 721 | FILE *f = funopen(ctx, 0, 0, 0, 0); |
| 722 | if (f == 0) { |
| 723 | free(ctx); |
| 724 | } |
| 725 | return f; // expected-warning{{leak}} |
| 726 | } |
| 727 | |
Jordan Rose | 85d7e01 | 2012-07-02 19:27:51 +0000 | [diff] [blame] | 728 | static int readNothing(void *_ctx, char *buf, int size) { |
| 729 | return 0; |
| 730 | } |
| 731 | FILE *useFunOpenReadNoRelease() { |
| 732 | void *ctx = malloc(sizeof(int)); |
| 733 | FILE *f = funopen(ctx, readNothing, 0, 0, 0); |
| 734 | if (f == 0) { |
| 735 | free(ctx); |
| 736 | } |
| 737 | return f; // expected-warning{{leak}} |
| 738 | } |
| 739 | |
Anna Zaks | ca23eb2 | 2012-02-29 18:42:47 +0000 | [diff] [blame] | 740 | // Test setbuf, setvbuf. |
| 741 | int my_main_no_warning() { |
| 742 | char *p = malloc(100); |
| 743 | setvbuf(stdout, p, 0, 100); |
| 744 | return 0; |
| 745 | } |
| 746 | int my_main_no_warning2() { |
| 747 | char *p = malloc(100); |
| 748 | setbuf(__stdoutp, p); |
| 749 | return 0; |
| 750 | } |
| 751 | int my_main_warn(FILE *f) { |
| 752 | char *p = malloc(100); |
| 753 | setvbuf(f, p, 0, 100); |
| 754 | return 0;// expected-warning {{leak}} |
| 755 | } |
| 756 | |
Ted Kremenek | a99f874 | 2012-03-05 23:06:19 +0000 | [diff] [blame] | 757 | // <rdar://problem/10978247>. |
| 758 | // some people use stack allocated memory as an optimization to avoid |
| 759 | // a heap allocation for small work sizes. This tests the analyzer's |
| 760 | // understanding that the malloc'ed memory is not the same as stackBuffer. |
| 761 | void radar10978247(int myValueSize) { |
| 762 | char stackBuffer[128]; |
| 763 | char *buffer; |
| 764 | |
| 765 | if (myValueSize <= sizeof(stackBuffer)) |
| 766 | buffer = stackBuffer; |
| 767 | else |
| 768 | buffer = malloc(myValueSize); |
| 769 | |
| 770 | // do stuff with the buffer |
| 771 | if (buffer != stackBuffer) |
| 772 | free(buffer); |
| 773 | } |
| 774 | |
| 775 | void radar10978247_positive(int myValueSize) { |
| 776 | char stackBuffer[128]; |
| 777 | char *buffer; |
| 778 | |
| 779 | if (myValueSize <= sizeof(stackBuffer)) |
| 780 | buffer = stackBuffer; |
| 781 | else |
| 782 | buffer = malloc(myValueSize); |
| 783 | |
| 784 | // do stuff with the buffer |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 785 | if (buffer == stackBuffer) |
Ted Kremenek | a99f874 | 2012-03-05 23:06:19 +0000 | [diff] [blame] | 786 | return; |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 787 | else |
| 788 | return; // expected-warning {{leak}} |
| 789 | } |
Ted Kremenek | 8f40afb | 2012-04-26 05:08:26 +0000 | [diff] [blame] | 790 | // <rdar://problem/11269741> Previously this triggered a false positive |
| 791 | // because malloc() is known to return uninitialized memory and the binding |
| 792 | // of 'o' to 'p->n' was not getting propertly handled. Now we report a leak. |
| 793 | struct rdar11269741_a_t { |
| 794 | struct rdar11269741_b_t { |
| 795 | int m; |
| 796 | } n; |
| 797 | }; |
| 798 | |
| 799 | int rdar11269741(struct rdar11269741_b_t o) |
| 800 | { |
| 801 | struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p)); |
| 802 | p->n = o; |
| 803 | return p->n.m; // expected-warning {{leak}} |
| 804 | } |
| 805 | |
Anna Zaks | e55a14a | 2012-05-03 02:13:56 +0000 | [diff] [blame] | 806 | // Pointer arithmetic, returning an ElementRegion. |
| 807 | void *radar11329382(unsigned bl) { |
| 808 | void *ptr = malloc (16); |
| 809 | ptr = ptr + (2 - bl); |
| 810 | return ptr; // no warning |
| 811 | } |
| 812 | |
Anna Zaks | 33e4a1d | 2012-05-01 21:10:29 +0000 | [diff] [blame] | 813 | void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__)); |
| 814 | int strcmp(const char *, const char *); |
| 815 | char *a (void); |
| 816 | void radar11270219(void) { |
| 817 | char *x = a(), *y = a(); |
| 818 | (__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0); |
| 819 | strcmp(x, y); // no warning |
| 820 | } |
| 821 | |
Anna Zaks | 93c5a24 | 2012-05-02 00:05:20 +0000 | [diff] [blame] | 822 | void radar_11358224_test_double_assign_ints_positive_2() |
| 823 | { |
| 824 | void *ptr = malloc(16); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 825 | ptr = ptr; |
| 826 | } // expected-warning {{leak}} |
Anna Zaks | 93c5a24 | 2012-05-02 00:05:20 +0000 | [diff] [blame] | 827 | |
Anna Zaks | aca0ac5 | 2012-05-03 23:50:28 +0000 | [diff] [blame] | 828 | // Assume that functions which take a function pointer can free memory even if |
| 829 | // they are defined in system headers and take the const pointer to the |
| 830 | // allocated memory. (radar://11160612) |
| 831 | int const_ptr_and_callback(int, const char*, int n, void(*)(void*)); |
| 832 | void r11160612_1() { |
| 833 | char *x = malloc(12); |
| 834 | const_ptr_and_callback(0, x, 12, free); // no - warning |
| 835 | } |
| 836 | |
| 837 | // Null is passed as callback. |
| 838 | void r11160612_2() { |
| 839 | char *x = malloc(12); |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 840 | const_ptr_and_callback(0, x, 12, 0); |
| 841 | } // expected-warning {{leak}} |
Anna Zaks | aca0ac5 | 2012-05-03 23:50:28 +0000 | [diff] [blame] | 842 | |
| 843 | // Callback is passed to a function defined in a system header. |
| 844 | void r11160612_4() { |
| 845 | char *x = malloc(12); |
| 846 | sqlite3_bind_text_my(0, x, 12, free); // no - warning |
| 847 | } |
| 848 | |
Anna Zaks | b79d862 | 2012-05-03 23:50:33 +0000 | [diff] [blame] | 849 | // Passing callbacks in a struct. |
| 850 | void r11160612_5(StWithCallback St) { |
| 851 | void *x = malloc(12); |
| 852 | dealocateMemWhenDoneByVal(x, St); |
| 853 | } |
| 854 | void r11160612_6(StWithCallback St) { |
| 855 | void *x = malloc(12); |
| 856 | dealocateMemWhenDoneByRef(&St, x); |
| 857 | } |
| 858 | |
Anna Zaks | 84d4384 | 2012-05-04 17:37:16 +0000 | [diff] [blame] | 859 | int mySub(int, int); |
| 860 | int myAdd(int, int); |
| 861 | int fPtr(unsigned cond, int x) { |
| 862 | return (cond ? mySub : myAdd)(x, x); |
| 863 | } |
| 864 | |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 865 | // Test anti-aliasing. |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 866 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 867 | void dependsOnValueOfPtr(int *g, unsigned f) { |
| 868 | int *p; |
| 869 | |
| 870 | if (f) { |
| 871 | p = g; |
| 872 | } else { |
| 873 | p = malloc(12); |
| 874 | } |
| 875 | |
| 876 | if (p != g) |
| 877 | free(p); |
| 878 | else |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 879 | return; // no warning |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 880 | return; |
| 881 | } |
| 882 | |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 883 | int CMPRegionHeapToStack() { |
| 884 | int x = 0; |
| 885 | int *x1 = malloc(8); |
| 886 | int *x2 = &x; |
Anna Zaks | adccc3f | 2012-06-08 00:04:40 +0000 | [diff] [blame] | 887 | clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}} |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 888 | free(x1); |
| 889 | return x; |
| 890 | } |
| 891 | |
| 892 | int CMPRegionHeapToHeap2() { |
| 893 | int x = 0; |
| 894 | int *x1 = malloc(8); |
| 895 | int *x2 = malloc(8); |
| 896 | int *x4 = x1; |
| 897 | int *x5 = x2; |
Anna Zaks | adccc3f | 2012-06-08 00:04:40 +0000 | [diff] [blame] | 898 | clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}} |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 899 | free(x1); |
| 900 | free(x2); |
| 901 | return x; |
| 902 | } |
| 903 | |
| 904 | int CMPRegionHeapToHeap() { |
| 905 | int x = 0; |
| 906 | int *x1 = malloc(8); |
| 907 | int *x4 = x1; |
| 908 | if (x1 == x4) { |
| 909 | free(x1); |
| 910 | return 5/x; // expected-warning{{Division by zero}} |
| 911 | } |
| 912 | return x;// expected-warning{{This statement is never executed}} |
| 913 | } |
| 914 | |
| 915 | int HeapAssignment() { |
| 916 | int m = 0; |
| 917 | int *x = malloc(4); |
| 918 | int *y = x; |
| 919 | *x = 5; |
Anna Zaks | adccc3f | 2012-06-08 00:04:40 +0000 | [diff] [blame] | 920 | clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}} |
Anna Zaks | e17fdb2 | 2012-06-07 03:57:32 +0000 | [diff] [blame] | 921 | free(x); |
| 922 | return 0; |
| 923 | } |
| 924 | |
Anna Zaks | 783f008 | 2012-06-07 20:18:08 +0000 | [diff] [blame] | 925 | int *retPtr(); |
| 926 | int *retPtrMightAlias(int *x); |
| 927 | int cmpHeapAllocationToUnknown() { |
| 928 | int zero = 0; |
| 929 | int *yBefore = retPtr(); |
| 930 | int *m = malloc(8); |
| 931 | int *yAfter = retPtrMightAlias(m); |
Anna Zaks | adccc3f | 2012-06-08 00:04:40 +0000 | [diff] [blame] | 932 | clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}} |
| 933 | clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}} |
Anna Zaks | 783f008 | 2012-06-07 20:18:08 +0000 | [diff] [blame] | 934 | free(m); |
| 935 | return 0; |
| 936 | } |
| 937 | |
Jordan Rose | 74f6982 | 2013-03-20 20:35:57 +0000 | [diff] [blame] | 938 | void localArrayTest() { |
| 939 | char *p = (char*)malloc(12); |
| 940 | char *ArrayL[12]; |
| 941 | ArrayL[0] = p; |
| 942 | } // expected-warning {{leak}} |
| 943 | |
| 944 | void localStructTest() { |
| 945 | StructWithPtr St; |
| 946 | StructWithPtr *pSt = &St; |
| 947 | pSt->memP = malloc(12); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 948 | } // expected-warning{{Potential leak of memory pointed to by}} |
Jordan Rose | 74f6982 | 2013-03-20 20:35:57 +0000 | [diff] [blame] | 949 | |
Jordan Rose | 6e99f9f | 2012-11-27 02:37:49 +0000 | [diff] [blame] | 950 | #ifdef __INTPTR_TYPE__ |
Ted Kremenek | 140d0c6 | 2012-05-01 21:58:29 +0000 | [diff] [blame] | 951 | // Test double assignment through integers. |
Jordan Rose | 6e99f9f | 2012-11-27 02:37:49 +0000 | [diff] [blame] | 952 | typedef __INTPTR_TYPE__ intptr_t; |
| 953 | typedef unsigned __INTPTR_TYPE__ uintptr_t; |
| 954 | |
| 955 | static intptr_t glob; |
Ted Kremenek | 140d0c6 | 2012-05-01 21:58:29 +0000 | [diff] [blame] | 956 | void test_double_assign_ints() |
| 957 | { |
| 958 | void *ptr = malloc (16); // no-warning |
Jordan Rose | 6e99f9f | 2012-11-27 02:37:49 +0000 | [diff] [blame] | 959 | glob = (intptr_t)(uintptr_t)ptr; |
Ted Kremenek | 140d0c6 | 2012-05-01 21:58:29 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | void test_double_assign_ints_positive() |
| 963 | { |
| 964 | void *ptr = malloc(16); |
Jordan Rose | 6e99f9f | 2012-11-27 02:37:49 +0000 | [diff] [blame] | 965 | (void*)(intptr_t)(uintptr_t)ptr; // expected-warning {{unused}} |
Jordan Rose | 63bc186 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 966 | } // expected-warning {{leak}} |
Jordan Rose | 6e99f9f | 2012-11-27 02:37:49 +0000 | [diff] [blame] | 967 | #endif |
Jordan Rose | 1bf908d | 2012-06-16 00:09:20 +0000 | [diff] [blame] | 968 | |
| 969 | void testCGContextNoLeak() |
| 970 | { |
| 971 | void *ptr = malloc(16); |
| 972 | CGContextRef context = CGBitmapContextCreate(ptr); |
| 973 | |
| 974 | // Because you can get the data back out like this, even much later, |
| 975 | // CGBitmapContextCreate is one of our "stop-tracking" exceptions. |
| 976 | free(CGBitmapContextGetData(context)); |
| 977 | } |
| 978 | |
| 979 | void testCGContextLeak() |
| 980 | { |
| 981 | void *ptr = malloc(16); |
| 982 | CGContextRef context = CGBitmapContextCreate(ptr); |
| 983 | // However, this time we're just leaking the data, because the context |
| 984 | // object doesn't escape and it hasn't been freed in this function. |
| 985 | } |
| 986 | |
Anna Zaks | 52a0481 | 2012-06-20 23:35:57 +0000 | [diff] [blame] | 987 | // Allow xpc context to escape. radar://11635258 |
| 988 | // TODO: Would be great if we checked that the finalize_connection_context actually releases it. |
| 989 | static void finalize_connection_context(void *ctx) { |
| 990 | int *context = ctx; |
| 991 | free(context); |
| 992 | } |
| 993 | void foo (xpc_connection_t peer) { |
| 994 | int *ctx = calloc(1, sizeof(int)); |
| 995 | xpc_connection_set_context(peer, ctx); |
| 996 | xpc_connection_set_finalizer_f(peer, finalize_connection_context); |
| 997 | xpc_connection_resume(peer); |
| 998 | } |
| 999 | |
Anna Zaks | ede875b | 2012-08-03 18:30:18 +0000 | [diff] [blame] | 1000 | // Make sure we catch errors when we free in a function which does not allocate memory. |
| 1001 | void freeButNoMalloc(int *p, int x){ |
| 1002 | if (x) { |
| 1003 | free(p); |
| 1004 | //user forgot a return here. |
| 1005 | } |
| 1006 | free(p); // expected-warning {{Attempt to free released memory}} |
| 1007 | } |
Anna Zaks | 4d33286 | 2012-08-04 02:04:27 +0000 | [diff] [blame] | 1008 | |
| 1009 | struct HasPtr { |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1010 | char *p; |
Anna Zaks | 4d33286 | 2012-08-04 02:04:27 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1013 | char* reallocButNoMalloc(struct HasPtr *a, int c, int size) { |
Anna Zaks | 4d33286 | 2012-08-04 02:04:27 +0000 | [diff] [blame] | 1014 | int *s; |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1015 | char *b = realloc(a->p, size); |
| 1016 | char *m = realloc(a->p, size); // expected-warning {{Attempt to free released memory}} |
Anna Zaks | 4d33286 | 2012-08-04 02:04:27 +0000 | [diff] [blame] | 1017 | return a->p; |
| 1018 | } |
Jordan Rose | 0d53ab4 | 2012-08-08 18:23:31 +0000 | [diff] [blame] | 1019 | |
Anna Zaks | 55dd956 | 2012-08-24 02:28:20 +0000 | [diff] [blame] | 1020 | // We should not warn in this case since the caller will presumably free a->p in all cases. |
| 1021 | int reallocButNoMallocPR13674(struct HasPtr *a, int c, int size) { |
| 1022 | int *s; |
| 1023 | char *b = realloc(a->p, size); |
| 1024 | if (b == 0) |
| 1025 | return -1; |
| 1026 | a->p = b; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Anna Zaks | 9dc298b | 2012-09-12 22:57:34 +0000 | [diff] [blame] | 1030 | // Test realloc with no visible malloc. |
| 1031 | void *test(void *ptr) { |
| 1032 | void *newPtr = realloc(ptr, 4); |
| 1033 | if (newPtr == 0) { |
| 1034 | if (ptr) |
| 1035 | free(ptr); // no-warning |
| 1036 | } |
| 1037 | return newPtr; |
| 1038 | } |
| 1039 | |
Jordan Rose | 84c4845 | 2012-11-15 19:11:27 +0000 | [diff] [blame] | 1040 | |
| 1041 | char *testLeakWithinReturn(char *str) { |
| 1042 | return strdup(strdup(str)); // expected-warning{{leak}} |
| 1043 | } |
| 1044 | |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1045 | void passConstPtr(const char * ptr); |
| 1046 | |
| 1047 | void testPassConstPointer() { |
| 1048 | char * string = malloc(sizeof(char)*10); |
| 1049 | passConstPtr(string); |
| 1050 | return; // expected-warning {{leak}} |
| 1051 | } |
| 1052 | |
| 1053 | void testPassConstPointerIndirectly() { |
| 1054 | char *p = malloc(1); |
| 1055 | p++; |
| 1056 | memcmp(p, p, sizeof(&p)); |
| 1057 | return; // expected-warning {{leak}} |
| 1058 | } |
| 1059 | |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1060 | void testPassConstPointerIndirectlyStruct() { |
| 1061 | struct HasPtr hp; |
| 1062 | hp.p = malloc(10); |
| 1063 | memcmp(&hp, &hp, sizeof(hp)); |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 1064 | return; // expected-warning {{Potential leak of memory pointed to by 'hp.p'}} |
Anna Zaks | 233e26a | 2013-02-07 23:05:43 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | void testPassToSystemHeaderFunctionIndirectlyStruct() { |
| 1068 | SomeStruct ss; |
| 1069 | ss.p = malloc(1); |
Jordan Rose | 374ae32 | 2013-05-10 17:07:16 +0000 | [diff] [blame] | 1070 | fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable |
| 1071 | // Technically a false negative here -- we know the system function won't free |
| 1072 | // ss.p, but nothing else will either! |
| 1073 | } // no-warning |
| 1074 | |
| 1075 | void testPassToSystemHeaderFunctionIndirectlyStructFree() { |
| 1076 | SomeStruct ss; |
| 1077 | ss.p = malloc(1); |
| 1078 | fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable |
| 1079 | free(ss.p); |
| 1080 | } // no-warning |
| 1081 | |
| 1082 | void testPassToSystemHeaderFunctionIndirectlyArray() { |
| 1083 | int *p[1]; |
| 1084 | p[0] = malloc(sizeof(int)); |
| 1085 | fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable |
| 1086 | // Technically a false negative here -- we know the system function won't free |
| 1087 | // p[0], but nothing else will either! |
| 1088 | } // no-warning |
| 1089 | |
| 1090 | void testPassToSystemHeaderFunctionIndirectlyArrayFree() { |
| 1091 | int *p[1]; |
| 1092 | p[0] = malloc(sizeof(int)); |
| 1093 | fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable |
| 1094 | free(p[0]); |
| 1095 | } // no-warning |
Anna Zaks | b98c6fe | 2013-02-06 00:01:14 +0000 | [diff] [blame] | 1096 | |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 1097 | int *testOffsetAllocate(size_t size) { |
| 1098 | int *memoryBlock = (int *)malloc(size + sizeof(int)); |
| 1099 | return &memoryBlock[1]; // no-warning |
| 1100 | } |
| 1101 | |
| 1102 | void testOffsetDeallocate(int *memoryBlock) { |
| 1103 | free(&memoryBlock[-1]); // no-warning |
| 1104 | } |
| 1105 | |
| 1106 | void testOffsetOfRegionFreed() { |
| 1107 | __int64_t * array = malloc(sizeof(__int64_t)*2); |
| 1108 | array += 1; |
| 1109 | free(&array[0]); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}} |
| 1110 | } |
| 1111 | |
| 1112 | void testOffsetOfRegionFreed2() { |
| 1113 | __int64_t *p = malloc(sizeof(__int64_t)*2); |
| 1114 | p += 1; |
| 1115 | free(p); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}} |
| 1116 | } |
| 1117 | |
| 1118 | void testOffsetOfRegionFreed3() { |
| 1119 | char *r = malloc(sizeof(char)); |
| 1120 | r = r - 10; |
| 1121 | free(r); // expected-warning {{Argument to free() is offset by -10 bytes from the start of memory allocated by malloc()}} |
| 1122 | } |
| 1123 | |
| 1124 | void testOffsetOfRegionFreedAfterFunctionCall() { |
| 1125 | int *p = malloc(sizeof(int)*2); |
| 1126 | p += 1; |
| 1127 | myfoo(p); |
Anna Zaks | 0413023 | 2013-04-09 00:30:28 +0000 | [diff] [blame] | 1128 | free(p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}} |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | void testFixManipulatedPointerBeforeFree() { |
| 1132 | int * array = malloc(sizeof(int)*2); |
| 1133 | array += 1; |
| 1134 | free(&array[-1]); // no-warning |
| 1135 | } |
| 1136 | |
| 1137 | void testFixManipulatedPointerBeforeFree2() { |
| 1138 | char *r = malloc(sizeof(char)); |
| 1139 | r = r + 10; |
| 1140 | free(r-10); // no-warning |
| 1141 | } |
| 1142 | |
| 1143 | void freeOffsetPointerPassedToFunction() { |
| 1144 | __int64_t *p = malloc(sizeof(__int64_t)*2); |
| 1145 | p[1] = 0; |
| 1146 | p += 1; |
| 1147 | myfooint(*p); // not passing the pointer, only a value pointed by pointer |
| 1148 | free(p); // expected-warning {{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}} |
| 1149 | } |
| 1150 | |
| 1151 | int arbitraryInt(); |
| 1152 | void freeUnknownOffsetPointer() { |
| 1153 | char *r = malloc(sizeof(char)); |
| 1154 | r = r + arbitraryInt(); // unable to reason about what the offset might be |
| 1155 | free(r); // no-warning |
| 1156 | } |
| 1157 | |
| 1158 | void testFreeNonMallocPointerWithNoOffset() { |
| 1159 | char c; |
| 1160 | char *r = &c; |
| 1161 | r = r + 10; |
| 1162 | free(r-10); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}} |
| 1163 | } |
| 1164 | |
| 1165 | void testFreeNonMallocPointerWithOffset() { |
| 1166 | char c; |
| 1167 | char *r = &c; |
| 1168 | free(r+1); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}} |
| 1169 | } |
| 1170 | |
| 1171 | void testOffsetZeroDoubleFree() { |
| 1172 | int *array = malloc(sizeof(int)*2); |
| 1173 | int *p = &array[0]; |
| 1174 | free(p); |
| 1175 | free(&array[0]); // expected-warning{{Attempt to free released memory}} |
| 1176 | } |
| 1177 | |
| 1178 | void testOffsetPassedToStrlen() { |
| 1179 | char * string = malloc(sizeof(char)*10); |
| 1180 | string += 1; |
Anna Zaks | 68eb4c2 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 1181 | int length = strlen(string); // expected-warning {{Potential leak of memory pointed to by 'string'}} |
Anna Zaks | 118aa75 | 2013-02-07 23:05:47 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | void testOffsetPassedToStrlenThenFree() { |
| 1185 | char * string = malloc(sizeof(char)*10); |
| 1186 | string += 1; |
| 1187 | int length = strlen(string); |
| 1188 | free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}} |
| 1189 | } |
| 1190 | |
| 1191 | void testOffsetPassedAsConst() { |
| 1192 | char * string = malloc(sizeof(char)*10); |
| 1193 | string += 1; |
| 1194 | passConstPtr(string); |
| 1195 | free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}} |
| 1196 | } |
Anna Zaks | b98c6fe | 2013-02-06 00:01:14 +0000 | [diff] [blame] | 1197 | |
Anna Zaks | 74c0d69 | 2013-03-15 23:34:29 +0000 | [diff] [blame] | 1198 | char **_vectorSegments; |
| 1199 | int _nVectorSegments; |
| 1200 | |
| 1201 | void poolFreeC(void* s) { |
| 1202 | free(s); // no-warning |
| 1203 | } |
| 1204 | void freeMemory() { |
| 1205 | while (_nVectorSegments) { |
| 1206 | poolFreeC(_vectorSegments[_nVectorSegments++]); |
| 1207 | } |
| 1208 | } |
Jordan Rose | 74f6982 | 2013-03-20 20:35:57 +0000 | [diff] [blame] | 1209 | |
| 1210 | // ---------------------------------------------------------------------------- |
| 1211 | // False negatives. |
| 1212 | |
| 1213 | void testMallocWithParam(int **p) { |
| 1214 | *p = (int*) malloc(sizeof(int)); |
| 1215 | *p = 0; // FIXME: should warn here |
| 1216 | } |
| 1217 | |
| 1218 | void testMallocWithParam_2(int **p) { |
| 1219 | *p = (int*) malloc(sizeof(int)); // no-warning |
| 1220 | } |
Jordan Rose | 374ae32 | 2013-05-10 17:07:16 +0000 | [diff] [blame] | 1221 | |
| 1222 | void testPassToSystemHeaderFunctionIndirectly() { |
| 1223 | int *p = malloc(4); |
| 1224 | p++; |
| 1225 | fakeSystemHeaderCallInt(p); |
| 1226 | // FIXME: This is a leak: if we think a system function won't free p, it |
| 1227 | // won't free (p-1) either. |
| 1228 | } |