2012-09-08 12:26:37 +08:00
|
|
|
// RUN: rm -f %t
|
2018-08-30 04:29:17 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,unix.Malloc -analyzer-output=plist -verify -o %t -analyzer-config eagerly-assume=false %s
|
2019-06-11 22:21:32 +08:00
|
|
|
// RUN: tail -n +11 %t | %normalize_plist | diff -ub %S/Inputs/expected-plists/malloc-plist.c.plist -
|
2012-02-16 11:41:01 +08:00
|
|
|
|
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
void *malloc(size_t);
|
2012-02-17 06:26:07 +08:00
|
|
|
void free(void *);
|
|
|
|
void *realloc(void *ptr, size_t size);
|
2012-02-16 11:41:01 +08:00
|
|
|
|
|
|
|
void diagnosticTest(int in) {
|
|
|
|
if (in > 5) {
|
|
|
|
int *p = malloc(12);
|
2017-11-30 17:18:35 +08:00
|
|
|
*p = 0;
|
2012-02-16 11:41:01 +08:00
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
in++; // expected-warning {{leak}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void myArrayAllocation(void) {
|
2012-02-16 11:41:01 +08:00
|
|
|
int **A;
|
|
|
|
A = malloc(2*sizeof(int*));
|
2018-06-27 09:51:36 +08:00
|
|
|
A[0] = 0;
|
|
|
|
}//expected-warning{{Potential leak}}
|
2012-02-16 11:41:01 +08:00
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void reallocDiagnostics(void) {
|
2012-02-17 06:26:07 +08:00
|
|
|
char * buf = malloc(100);
|
|
|
|
char * tmp;
|
|
|
|
tmp = (char*)realloc(buf, 0x1000000);
|
|
|
|
if (!tmp) {
|
|
|
|
return;// expected-warning {{leak}}
|
|
|
|
}
|
|
|
|
buf = tmp;
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void *wrapper(void) {
|
2012-03-09 09:13:14 +08:00
|
|
|
void *x = malloc(100);
|
|
|
|
// This is intentionally done to test diagnostic emission.
|
|
|
|
if (x)
|
|
|
|
return x;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void test_wrapper(void) {
|
2012-03-09 09:13:14 +08:00
|
|
|
void *buf = wrapper();
|
|
|
|
(void) buf;
|
2018-06-27 09:51:36 +08:00
|
|
|
}//expected-warning{{Potential leak}}
|
2012-03-09 09:13:14 +08:00
|
|
|
|
2012-03-16 05:13:02 +08:00
|
|
|
// Test what happens when the same call frees and allocated memory.
|
2012-03-17 07:24:20 +08:00
|
|
|
// Also tests the stack hint for parameters, when they are passed directly or via pointer.
|
2012-03-16 05:13:02 +08:00
|
|
|
void my_free(void *x) {
|
|
|
|
free(x);
|
|
|
|
}
|
|
|
|
void my_malloc_and_free(void **x) {
|
|
|
|
*x = malloc(100);
|
|
|
|
if (*x)
|
|
|
|
my_free(*x);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void *test_double_action_call(void) {
|
2012-03-16 05:13:02 +08:00
|
|
|
void *buf;
|
|
|
|
my_malloc_and_free(&buf);
|
2018-06-27 09:51:36 +08:00
|
|
|
return buf; //expected-warning{{Use of memory after it is freed}}
|
2012-03-16 05:13:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-17 07:24:20 +08:00
|
|
|
// Test stack hint for 'reallocation failed'.
|
|
|
|
char *my_realloc(char *buf) {
|
|
|
|
char *tmp;
|
|
|
|
tmp = (char*)realloc(buf, 0x1000000);
|
|
|
|
if (!tmp) {
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void reallocIntra(void) {
|
2012-03-17 07:24:20 +08:00
|
|
|
char *buf = (char *)malloc(100);
|
|
|
|
buf = my_realloc(buf);
|
2018-06-27 09:51:36 +08:00
|
|
|
free(buf);//expected-warning{{Potential leak}}
|
2012-03-17 07:24:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test stack hint when returning a result.
|
2022-02-13 21:02:46 +08:00
|
|
|
static char *malloc_wrapper_ret(void) {
|
2012-03-17 07:24:20 +08:00
|
|
|
return (char*)malloc(12);
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_ret(void) {
|
2012-03-17 07:24:20 +08:00
|
|
|
char *v;
|
|
|
|
v = malloc_wrapper_ret();
|
2018-06-27 09:51:36 +08:00
|
|
|
}//expected-warning{{Potential leak}}
|
2012-03-17 07:24:20 +08:00
|
|
|
|
2015-11-06 02:56:42 +08:00
|
|
|
// Passing a block as a parameter to an inlined call for which we generate
|
|
|
|
// a stack hint message caused crashes.
|
|
|
|
// rdar://problem/21291971
|
|
|
|
void myfree_takingblock(void (^ignored)(void), int *p) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
void call_myfree_takingblock(void) {
|
2015-11-06 02:56:42 +08:00
|
|
|
void (^some_block)(void) = ^void(void) { };
|
|
|
|
|
|
|
|
int *p = malloc(sizeof(int));
|
|
|
|
myfree_takingblock(some_block, p);
|
2018-06-27 09:51:36 +08:00
|
|
|
*p = 3;//expected-warning{{Use of memory after it is freed}}
|
2015-11-06 02:56:42 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 03:45:08 +08:00
|
|
|
// Test that we refer to the last symbol used in the leak diagnostic.
|
|
|
|
void LeakedSymbol(int in) {
|
|
|
|
int *m = 0;
|
|
|
|
int *p;
|
|
|
|
p = (int*)malloc(12);
|
2017-11-30 17:18:35 +08:00
|
|
|
*p = 0;
|
2012-03-22 03:45:08 +08:00
|
|
|
(*p)++;
|
|
|
|
m = p;
|
|
|
|
p = 0;
|
|
|
|
(*m)++;
|
2018-06-27 09:51:36 +08:00
|
|
|
in++;//expected-warning{{Potential leak}}
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
2012-03-17 07:44:28 +08:00
|
|
|
|
2012-04-21 05:59:08 +08:00
|
|
|
// Tests that exercise running remove dead bindings at Call exit.
|
2022-02-13 21:02:46 +08:00
|
|
|
static void function_with_leak1(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
char *x = (char*)malloc(12);
|
2018-06-27 09:51:36 +08:00
|
|
|
} //expected-warning{{Potential leak}}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_function_with_leak1(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
function_with_leak1();
|
|
|
|
int y = 0;
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
static void function_with_leak2(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
char *x = (char*)malloc(12);
|
2018-06-27 09:51:36 +08:00
|
|
|
int m = 0; //expected-warning{{Potential leak}}
|
2012-04-21 05:59:08 +08:00
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_function_with_leak2(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
function_with_leak2();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void function_with_leak3(int y) {
|
|
|
|
char *x = (char*)malloc(12);
|
|
|
|
if (y)
|
|
|
|
y++;
|
2018-06-27 09:51:36 +08:00
|
|
|
}//expected-warning{{Potential leak}}
|
2012-04-21 05:59:08 +08:00
|
|
|
void use_function_with_leak3(int y) {
|
|
|
|
function_with_leak3(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void function_with_leak4(int y) {
|
|
|
|
char *x = (char*)malloc(12);
|
|
|
|
if (y)
|
|
|
|
y++;
|
|
|
|
else
|
2018-06-27 09:51:36 +08:00
|
|
|
y--;//expected-warning{{Potential leak}}
|
2012-04-21 05:59:08 +08:00
|
|
|
}
|
|
|
|
void use_function_with_leak4(int y) {
|
|
|
|
function_with_leak4(y);
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
int anotherFunction5(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
return 5;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
static int function_with_leak5(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
char *x = (char*)malloc(12);
|
2018-06-27 09:51:36 +08:00
|
|
|
return anotherFunction5();//expected-warning{{Potential leak}}
|
2012-04-21 05:59:08 +08:00
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_function_with_leak5(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
function_with_leak5();
|
|
|
|
}
|
|
|
|
|
|
|
|
void anotherFunction6(int m) {
|
|
|
|
m++;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
static void function_with_leak6(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
char *x = (char*)malloc(12);
|
2018-06-27 09:51:36 +08:00
|
|
|
anotherFunction6(3);//expected-warning{{Potential leak}}
|
2012-04-21 05:59:08 +08:00
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_function_with_leak6(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
function_with_leak6();
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
static void empty_function(void){
|
2012-04-21 05:59:08 +08:00
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_empty_function(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
empty_function();
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
static char *function_with_leak7(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
return (char*)malloc(12);
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void use_function_with_leak7(void) {
|
2012-04-21 05:59:08 +08:00
|
|
|
function_with_leak7();
|
2018-06-27 09:51:36 +08:00
|
|
|
}//expected-warning{{Potential memory leak}}
|
2012-04-05 02:11:35 +08:00
|
|
|
|
2013-04-11 06:56:33 +08:00
|
|
|
// Test that we do not print the name of a variable not visible from where
|
|
|
|
// the issue is reported.
|
2022-02-13 21:02:46 +08:00
|
|
|
int *my_malloc(void) {
|
2013-04-11 05:42:02 +08:00
|
|
|
int *p = malloc(12);
|
|
|
|
return p;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void testOnlyRefferToVisibleVariables(void) {
|
2013-04-11 05:42:02 +08:00
|
|
|
my_malloc();
|
2018-06-27 09:51:36 +08:00
|
|
|
} // expected-warning{{Potential memory leak}}
|
2013-04-11 05:42:02 +08:00
|
|
|
|
2013-04-11 06:56:33 +08:00
|
|
|
struct PointerWrapper{
|
|
|
|
int*p;
|
|
|
|
};
|
2022-02-13 21:02:46 +08:00
|
|
|
int *my_malloc_into_struct(void) {
|
2013-04-11 06:56:33 +08:00
|
|
|
struct PointerWrapper w;
|
|
|
|
w.p = malloc(12);
|
|
|
|
return w.p;
|
|
|
|
}
|
2022-02-13 21:02:46 +08:00
|
|
|
void testMyMalloc(void) {
|
2018-06-27 09:51:36 +08:00
|
|
|
my_malloc_into_struct();
|
|
|
|
} // expected-warning{{Potential memory leak}}
|