Ted Kremenek | 8382cf5 | 2009-11-13 18:46:29 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s |
Eli Friedman | 2f00552 | 2009-11-14 04:23:25 +0000 | [diff] [blame] | 2 | typedef __typeof(sizeof(int)) size_t; |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 3 | void *malloc(size_t); |
| 4 | void free(void *); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 5 | |
| 6 | void f1() { |
| 7 | int *p = malloc(10); |
| 8 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 9 | } |
| 10 | |
Ted Kremenek | ba93087 | 2009-11-13 19:53:32 +0000 | [diff] [blame] | 11 | void f1_b() { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 12 | int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
Ted Kremenek | ba93087 | 2009-11-13 19:53:32 +0000 | [diff] [blame] | 13 | } |
| 14 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 15 | void f2() { |
| 16 | int *p = malloc(10); |
| 17 | free(p); |
| 18 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 19 | } |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 20 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 21 | // This case tests that storing malloc'ed memory to a static variable which is |
| 22 | // then returned is not leaked. In the absence of known contracts for functions |
| 23 | // or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 24 | int *f3() { |
| 25 | static int *p = 0; |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 26 | p = malloc(10); |
| 27 | return p; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 30 | // This case tests that storing malloc'ed memory to a static global variable |
| 31 | // which is then returned is not leaked. In the absence of known contracts for |
| 32 | // functions or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 33 | static int *p_f4 = 0; |
| 34 | int *f4() { |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 35 | p_f4 = malloc(10); |
| 36 | return p_f4; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 37 | } |