2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=unix.Malloc -analyzer-inline-max-stack-depth=5 -verify %s
|
2012-02-21 06:25:23 +08:00
|
|
|
|
2012-09-12 09:11:10 +08:00
|
|
|
#include "Inputs/system-header-simulator.h"
|
2012-02-21 06:25:23 +08:00
|
|
|
|
|
|
|
void *malloc(size_t);
|
|
|
|
void *valloc(size_t);
|
|
|
|
void free(void *);
|
|
|
|
void *realloc(void *ptr, size_t size);
|
|
|
|
void *reallocf(void *ptr, size_t size);
|
|
|
|
void *calloc(size_t nmemb, size_t size);
|
2012-09-20 09:55:32 +08:00
|
|
|
|
|
|
|
void exit(int) __attribute__ ((__noreturn__));
|
|
|
|
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
|
|
|
|
size_t strlen(const char *);
|
2012-02-21 06:25:23 +08:00
|
|
|
|
|
|
|
static void my_malloc1(void **d, size_t size) {
|
|
|
|
*d = malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *my_malloc2(int elevel, size_t size) {
|
|
|
|
void *data;
|
|
|
|
data = malloc(size);
|
|
|
|
if (data == 0)
|
|
|
|
exit(0);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void my_free1(void *p) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test1() {
|
|
|
|
void *data = 0;
|
2012-11-16 03:11:43 +08:00
|
|
|
my_malloc1(&data, 4);
|
2013-04-06 08:41:36 +08:00
|
|
|
} // expected-warning {{Potential leak of memory pointed to by 'data'}}
|
2012-02-21 06:25:23 +08:00
|
|
|
|
2012-02-21 08:00:48 +08:00
|
|
|
static void test11() {
|
|
|
|
void *data = 0;
|
|
|
|
my_malloc1(&data, 4);
|
|
|
|
my_free1(data);
|
|
|
|
}
|
|
|
|
|
2012-02-28 07:40:55 +08:00
|
|
|
static void testUniqueingByallocationSiteInTopLevelFunction() {
|
|
|
|
void *data = my_malloc2(1, 4);
|
|
|
|
data = 0;
|
2013-04-06 08:41:36 +08:00
|
|
|
int x = 5;// expected-warning {{Potential leak of memory pointed to by 'data'}}
|
2012-11-16 03:11:43 +08:00
|
|
|
data = my_malloc2(1, 4);
|
2013-04-06 08:41:36 +08:00
|
|
|
} // expected-warning {{Potential leak of memory pointed to by 'data'}}
|
2012-02-21 06:25:23 +08:00
|
|
|
|
|
|
|
static void test3() {
|
|
|
|
void *data = my_malloc2(1, 4);
|
|
|
|
free(data);
|
|
|
|
data = my_malloc2(1, 4);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int test4() {
|
|
|
|
int *data = (int*)my_malloc2(1, 4);
|
|
|
|
my_free1(data);
|
|
|
|
data = (int *)my_malloc2(1, 4);
|
|
|
|
my_free1(data);
|
|
|
|
return *data; // expected-warning {{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
2012-02-21 08:00:48 +08:00
|
|
|
void test6() {
|
|
|
|
int *data = (int *)my_malloc2(1, 4);
|
|
|
|
my_free1((int*)data);
|
|
|
|
my_free1((int*)data); // expected-warning{{Use of memory after it is freed}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should warn here.
|
|
|
|
void test5() {
|
|
|
|
int *data;
|
|
|
|
my_free1((int*)data);
|
|
|
|
}
|
2012-02-25 00:49:41 +08:00
|
|
|
|
2012-02-25 00:49:46 +08:00
|
|
|
static char *reshape(char *in) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testThatRemoveDeadBindingsRunBeforeEachCall() {
|
|
|
|
char *v = malloc(12);
|
|
|
|
v = reshape(v);
|
2013-04-06 08:41:36 +08:00
|
|
|
v = reshape(v);// expected-warning {{Potential leak of memory pointed to by 'v'}}
|
2012-02-25 00:49:46 +08:00
|
|
|
}
|
|
|
|
|
2012-02-25 00:49:41 +08:00
|
|
|
// Test that we keep processing after 'return;'
|
|
|
|
void fooWithEmptyReturn(int x) {
|
|
|
|
if (x)
|
|
|
|
return;
|
|
|
|
x++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uafAndCallsFooWithEmptyReturn() {
|
|
|
|
int *x = (int*)malloc(12);
|
|
|
|
free(x);
|
|
|
|
fooWithEmptyReturn(12);
|
|
|
|
return *x; // expected-warning {{Use of memory after it is freed}}
|
|
|
|
}
|
2012-09-20 09:55:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
// If we inline any of the malloc-family functions, the checker shouldn't also
|
|
|
|
// try to do additional modeling. <rdar://problem/12317671>
|
|
|
|
char *strndup(const char *str, size_t n) {
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// DO NOT FIX. This is to test that we are actually using the inlined
|
|
|
|
// behavior!
|
|
|
|
if (n < 5)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size_t length = strlen(str);
|
|
|
|
if (length < n)
|
|
|
|
n = length;
|
|
|
|
|
|
|
|
char *result = malloc(n + 1);
|
|
|
|
memcpy(result, str, n);
|
|
|
|
result[n] = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void useStrndup(size_t n) {
|
2012-11-16 03:11:43 +08:00
|
|
|
if (n == 0) {
|
2012-09-20 09:55:32 +08:00
|
|
|
(void)strndup(0, 20); // no-warning
|
2012-11-16 03:11:43 +08:00
|
|
|
return;
|
|
|
|
} else if (n < 5) {
|
2012-09-20 09:55:32 +08:00
|
|
|
(void)strndup("hi there", n); // no-warning
|
2012-11-16 03:11:43 +08:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
(void)strndup("hi there", n);
|
|
|
|
return; // expected-warning{{leak}}
|
|
|
|
}
|
2012-09-20 09:55:32 +08:00
|
|
|
}
|