Artem Dergachev | d69e012 | 2016-12-07 16:12:26 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s |
| 2 | // RUN: %clang_cc1 -triple i386-unknown-linux-gnu -w -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s |
Anna Zaks | 7e3e06a | 2012-03-29 23:26:54 +0000 | [diff] [blame] | 3 | |
| 4 | typedef __typeof(sizeof(int)) size_t; |
| 5 | void *malloc(size_t); |
| 6 | void free(void *); |
| 7 | void *realloc(void *ptr, size_t size); |
| 8 | void *calloc(size_t nmemb, size_t size); |
Anna Zaks | 60bf5f4 | 2013-04-02 01:28:24 +0000 | [diff] [blame] | 9 | char *strdup(const char *s); |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 10 | |
| 11 | void checkThatMallocCheckerIsRunning() { |
Jordan Rose | e37ab50 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 12 | malloc(4); |
| 13 | } // expected-warning{{leak}} |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 14 | |
Anna Zaks | 7e3e06a | 2012-03-29 23:26:54 +0000 | [diff] [blame] | 15 | // Test for radar://11110132. |
| 16 | struct Foo { |
| 17 | mutable void* m_data; |
| 18 | Foo(void* data) : m_data(data) {} |
| 19 | }; |
| 20 | Foo aFunction() { |
| 21 | return malloc(10); |
| 22 | } |
Anna Zaks | 228f9c7 | 2012-05-03 23:50:28 +0000 | [diff] [blame] | 23 | |
| 24 | // Assume that functions which take a function pointer can free memory even if |
| 25 | // they are defined in system headers and take the const pointer to the |
| 26 | // allocated memory. (radar://11160612) |
| 27 | // Test default parameter. |
Jordan Rose | 89bbd1f | 2013-05-01 23:10:44 +0000 | [diff] [blame] | 28 | int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = free); |
Anna Zaks | 228f9c7 | 2012-05-03 23:50:28 +0000 | [diff] [blame] | 29 | void r11160612_3() { |
| 30 | char *x = (char*)malloc(12); |
| 31 | const_ptr_and_callback_def_param(0, x, 12); |
| 32 | } |
Anna Zaks | 7577686 | 2012-05-04 22:18:36 +0000 | [diff] [blame] | 33 | |
Jordan Rose | 89bbd1f | 2013-05-01 23:10:44 +0000 | [diff] [blame] | 34 | int const_ptr_and_callback_def_param_null(int, const char*, int n, void(*)(void*) = 0); |
| 35 | void r11160612_no_callback() { |
| 36 | char *x = (char*)malloc(12); |
| 37 | const_ptr_and_callback_def_param_null(0, x, 12); |
| 38 | } // expected-warning{{leak}} |
| 39 | |
Anna Zaks | 7577686 | 2012-05-04 22:18:36 +0000 | [diff] [blame] | 40 | // Test member function pointer. |
| 41 | struct CanFreeMemory { |
| 42 | static void myFree(void*); |
| 43 | }; |
| 44 | //This is handled because we look at the type of the parameter(not argument). |
| 45 | void r11160612_3(CanFreeMemory* p) { |
| 46 | char *x = (char*)malloc(12); |
| 47 | const_ptr_and_callback_def_param(0, x, 12, p->myFree); |
| 48 | } |
| 49 | |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 50 | |
| 51 | namespace PR13751 { |
| 52 | class OwningVector { |
| 53 | void **storage; |
| 54 | size_t length; |
| 55 | public: |
| 56 | OwningVector(); |
| 57 | ~OwningVector(); |
| 58 | void push_back(void *Item) { |
| 59 | storage[length++] = Item; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | void testDestructors() { |
| 64 | OwningVector v; |
| 65 | v.push_back(malloc(4)); |
| 66 | // no leak warning; freed in destructor |
| 67 | } |
| 68 | } |
| 69 | |
Anna Zaks | 258f935 | 2013-02-06 00:01:14 +0000 | [diff] [blame] | 70 | struct X { void *a; }; |
| 71 | |
| 72 | struct X get() { |
| 73 | struct X result; |
| 74 | result.a = malloc(4); |
| 75 | return result; // no-warning |
| 76 | } |
Anna Zaks | 60bf5f4 | 2013-04-02 01:28:24 +0000 | [diff] [blame] | 77 | |
| 78 | // Ensure that regions accessible through a LazyCompoundVal trigger region escape. |
| 79 | // Malloc checker used to report leaks for the following two test cases. |
| 80 | struct Property { |
| 81 | char* getterName; |
| 82 | Property(char* n) |
| 83 | : getterName(n) {} |
| 84 | |
| 85 | }; |
| 86 | void append(Property x); |
| 87 | |
| 88 | void appendWrapper(char *getterName) { |
| 89 | append(Property(getterName)); |
| 90 | } |
| 91 | void foo(const char* name) { |
| 92 | char* getterName = strdup(name); |
| 93 | appendWrapper(getterName); // no-warning |
| 94 | } |
| 95 | |
| 96 | struct NestedProperty { |
| 97 | Property prop; |
| 98 | NestedProperty(Property p) |
| 99 | : prop(p) {} |
| 100 | }; |
| 101 | void appendNested(NestedProperty x); |
| 102 | |
| 103 | void appendWrapperNested(char *getterName) { |
| 104 | appendNested(NestedProperty(Property(getterName))); |
| 105 | } |
| 106 | void fooNested(const char* name) { |
| 107 | char* getterName = strdup(name); |
| 108 | appendWrapperNested(getterName); // no-warning |
Artem Dergachev | d69e012 | 2016-12-07 16:12:26 +0000 | [diff] [blame^] | 109 | } |
| 110 | |
| 111 | namespace PR31226 { |
| 112 | struct b2 { |
| 113 | int f; |
| 114 | }; |
| 115 | |
| 116 | struct b1 : virtual b2 { |
| 117 | void m(); |
| 118 | }; |
| 119 | |
| 120 | struct d : b1, b2 { |
| 121 | }; |
| 122 | |
| 123 | void f() { |
| 124 | d *p = new d(); |
| 125 | p->m(); // no-crash // no-warning |
| 126 | } |
| 127 | } |