malloc.c revision c360775fb7ed8352ca26f08c0270d21a6cb19e7f
1// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-experimental-checks -analyzer-store=region -verify %s
2typedef long unsigned 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
11// THIS TEST CURRENTLY FAILS.
12void f1_b() {
13  int *p = malloc(10);
14}
15
16void 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}
21
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}
39