Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-experimental-checks -analyzer-store=region -verify %s |
| 2 | typedef __typeof(sizeof(int)) size_t; |
| 3 | void *malloc(size_t); |
| 4 | void free(void *); |
| 5 | void *realloc(void *ptr, size_t size); |
| 6 | void *calloc(size_t nmemb, size_t size); |
| 7 | |
| 8 | void f1() { |
| 9 | int *p = malloc(10); |
| 10 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 11 | } |
| 12 | |
| 13 | void f1_b() { |
| 14 | int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 15 | } |
| 16 | |
| 17 | void f2() { |
| 18 | int *p = malloc(10); |
| 19 | free(p); |
| 20 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 21 | } |
| 22 | |
| 23 | // This case tests that storing malloc'ed memory to a static variable which is |
| 24 | // then returned is not leaked. In the absence of known contracts for functions |
| 25 | // or inter-procedural analysis, this is a conservative answer. |
| 26 | int *f3() { |
| 27 | static int *p = 0; |
| 28 | p = malloc(10); |
| 29 | return p; // no-warning |
| 30 | } |
| 31 | |
| 32 | // This case tests that storing malloc'ed memory to a static global variable |
| 33 | // which is then returned is not leaked. In the absence of known contracts for |
| 34 | // functions or inter-procedural analysis, this is a conservative answer. |
| 35 | static int *p_f4 = 0; |
| 36 | int *f4() { |
| 37 | p_f4 = malloc(10); |
| 38 | return p_f4; // no-warning |
| 39 | } |
| 40 | |
| 41 | int *f5() { |
| 42 | int *q = malloc(10); |
| 43 | q = realloc(q, 20); |
| 44 | return q; // no-warning |
| 45 | } |
| 46 | |
| 47 | void f6() { |
| 48 | int *p = malloc(10); |
| 49 | if (!p) |
| 50 | return; // no-warning |
| 51 | else |
| 52 | free(p); |
| 53 | } |
| 54 | |
| 55 | char *doit2(); |
| 56 | void pr6069() { |
| 57 | char *buf = doit2(); |
| 58 | free(buf); |
| 59 | } |