blob: fb9674a32b2a53edad4723a7f981fe86318555e8 [file] [log] [blame]
Ted Kremenek8382cf52009-11-13 18:46:29 +00001// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s
Eli Friedman2f005522009-11-14 04:23:25 +00002typedef __typeof(sizeof(int)) size_t;
Ted Kremenekc3607752009-11-13 20:03:22 +00003void *malloc(size_t);
4void free(void *);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +00005
6void f1() {
7 int *p = malloc(10);
8 return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
9}
10
Ted Kremenekba930872009-11-13 19:53:32 +000011// THIS TEST CURRENTLY FAILS.
12void f1_b() {
13 int *p = malloc(10);
14}
15
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000016void f2() {
17 int *p = malloc(10);
18 free(p);
19 free(p); // expected-warning{{Try to free a memory block that has been released}}
20}
Ted Kremenekc764d4b2009-11-13 20:00:28 +000021
22// This case tests that storing malloc'ed memory to a static variable which is then returned
23// is not leaked. In the absence of known contracts for functions or inter-procedural analysis,
24// this is a conservative answer.
25int *f3() {
26 static int *p = 0;
27 p = malloc(10); // no-warning
28 return p;
29}
30
31// This case tests that storing malloc'ed memory to a static global variable which is then returned
32// is not leaked. In the absence of known contracts for functions or inter-procedural analysis,
33// this is a conservative answer.
34static int *p_f4 = 0;
35int *f4() {
36 p_f4 = malloc(10); // no-warning
37 return p_f4;
38}