malloc.cpp revision f132ba8e571298ceda306c4484e031f990b347da
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,unix.Malloc -analyzer-store=region -verify %s
2
3typedef __typeof(sizeof(int)) size_t;
4void *malloc(size_t);
5void free(void *);
6void *realloc(void *ptr, size_t size);
7void *calloc(size_t nmemb, size_t size);
8
9// Test for radar://11110132.
10struct Foo {
11    mutable void* m_data;
12    Foo(void* data) : m_data(data) {}
13};
14Foo aFunction() {
15    return malloc(10);
16}
17
18// Assume that functions which take a function pointer can free memory even if
19// they are defined in system headers and take the const pointer to the
20// allocated memory. (radar://11160612)
21// Test default parameter.
22int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = 0);
23void r11160612_3() {
24  char *x = (char*)malloc(12);
25  const_ptr_and_callback_def_param(0, x, 12);
26}
27
28// Test member function pointer.
29struct CanFreeMemory {
30  static void myFree(void*);
31};
32//This is handled because we look at the type of the parameter(not argument).
33void r11160612_3(CanFreeMemory* p) {
34  char *x = (char*)malloc(12);
35  const_ptr_and_callback_def_param(0, x, 12, p->myFree);
36}
37
38