blob: 58b94ea774a4dcd0fd8107e9655d2e5d2a91d4bd [file] [log] [blame]
Ted Kremenekcdc3a892012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc -analyzer-store=region -verify %s
Anna Zaks4b81e742012-03-29 23:26:54 +00002
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
Jordan Rose5a1ffe92012-09-05 22:55:23 +00009
10void checkThatMallocCheckerIsRunning() {
Jordan Rose63bc1862012-11-15 19:11:43 +000011 malloc(4);
12} // expected-warning{{leak}}
Jordan Rose5a1ffe92012-09-05 22:55:23 +000013
Anna Zaks4b81e742012-03-29 23:26:54 +000014// Test for radar://11110132.
15struct Foo {
16 mutable void* m_data;
17 Foo(void* data) : m_data(data) {}
18};
19Foo aFunction() {
20 return malloc(10);
21}
Anna Zaksaca0ac52012-05-03 23:50:28 +000022
23// Assume that functions which take a function pointer can free memory even if
24// they are defined in system headers and take the const pointer to the
25// allocated memory. (radar://11160612)
26// Test default parameter.
27int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = 0);
28void r11160612_3() {
29 char *x = (char*)malloc(12);
30 const_ptr_and_callback_def_param(0, x, 12);
31}
Anna Zaksf132ba82012-05-04 22:18:36 +000032
33// Test member function pointer.
34struct CanFreeMemory {
35 static void myFree(void*);
36};
37//This is handled because we look at the type of the parameter(not argument).
38void r11160612_3(CanFreeMemory* p) {
39 char *x = (char*)malloc(12);
40 const_ptr_and_callback_def_param(0, x, 12, p->myFree);
41}
42
Jordan Rose5a1ffe92012-09-05 22:55:23 +000043
44namespace PR13751 {
45 class OwningVector {
46 void **storage;
47 size_t length;
48 public:
49 OwningVector();
50 ~OwningVector();
51 void push_back(void *Item) {
52 storage[length++] = Item;
53 }
54 };
55
56 void testDestructors() {
57 OwningVector v;
58 v.push_back(malloc(4));
59 // no leak warning; freed in destructor
60 }
61}
62