Ted Kremenek | cdc3a89 | 2012-08-24 20:39:55 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc -analyzer-store=region -verify %s |
Anna Zaks | 4b81e74 | 2012-03-29 23:26:54 +0000 | [diff] [blame] | 2 | |
| 3 | typedef __typeof(sizeof(int)) size_t; |
| 4 | void *malloc(size_t); |
| 5 | void free(void *); |
| 6 | void *realloc(void *ptr, size_t size); |
| 7 | void *calloc(size_t nmemb, size_t size); |
| 8 | |
| 9 | // Test for radar://11110132. |
| 10 | struct Foo { |
| 11 | mutable void* m_data; |
| 12 | Foo(void* data) : m_data(data) {} |
| 13 | }; |
| 14 | Foo aFunction() { |
| 15 | return malloc(10); |
| 16 | } |
Anna Zaks | aca0ac5 | 2012-05-03 23:50:28 +0000 | [diff] [blame] | 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. |
| 22 | int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = 0); |
| 23 | void r11160612_3() { |
| 24 | char *x = (char*)malloc(12); |
| 25 | const_ptr_and_callback_def_param(0, x, 12); |
| 26 | } |
Anna Zaks | f132ba8 | 2012-05-04 22:18:36 +0000 | [diff] [blame] | 27 | |
| 28 | // Test member function pointer. |
| 29 | struct CanFreeMemory { |
| 30 | static void myFree(void*); |
| 31 | }; |
| 32 | //This is handled because we look at the type of the parameter(not argument). |
| 33 | void r11160612_3(CanFreeMemory* p) { |
| 34 | char *x = (char*)malloc(12); |
| 35 | const_ptr_and_callback_def_param(0, x, 12, p->myFree); |
| 36 | } |
| 37 | |