malloc.c revision 243fde9f549a8f5f000c4baccb572dd0b7266a41
1// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s 2typedef __typeof(sizeof(int)) size_t; 3void *malloc(size_t); 4void free(void *); 5 6void f1() { 7 int *p = malloc(10); 8 return; // expected-warning{{Allocated memory never released. Potential memory leak.}} 9} 10 11void f1_b() { 12 int *p = malloc(10); // expected-warning{{Allocated memory never released. Potential memory leak.}} 13} 14 15void 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} 20 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. 24int *f3() { 25 static int *p = 0; 26 p = malloc(10); // will be fixed. 27 return p; // expected-warning{{Allocated memory never released. Potential memory leak.}} 28} 29 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. 33static int *p_f4 = 0; 34int *f4() { 35 p_f4 = malloc(10); // will be fixed. 36 return p_f4; // expected-warning{{Allocated memory never released. Potential memory leak.}} 37} 38