Jordan Rose | da5fc53 | 2012-07-26 20:04:00 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store region -analyzer-ipa=inlining -cfg-add-implicit-dtors -cfg-add-initializers -verify %s |
Zhongxing Xu | b13453b | 2010-11-20 06:53:12 +0000 | [diff] [blame] | 2 | |
| 3 | class A { |
| 4 | public: |
| 5 | ~A() { |
| 6 | int *x = 0; |
| 7 | *x = 3; // expected-warning{{Dereference of null pointer}} |
| 8 | } |
| 9 | }; |
| 10 | |
| 11 | int main() { |
| 12 | A a; |
| 13 | } |
Jordan Rose | da5fc53 | 2012-07-26 20:04:00 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | typedef __typeof(sizeof(int)) size_t; |
| 17 | void *malloc(size_t); |
| 18 | void free(void *); |
| 19 | |
| 20 | class SmartPointer { |
| 21 | void *X; |
| 22 | public: |
| 23 | SmartPointer(void *x) : X(x) {} |
| 24 | ~SmartPointer() { |
| 25 | free(X); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | void testSmartPointer() { |
| 30 | char *mem = (char*)malloc(4); |
| 31 | { |
| 32 | SmartPointer Deleter(mem); |
| 33 | // destructor called here |
| 34 | } |
| 35 | *mem = 0; // expected-warning{{Use of memory after it is freed}} |
| 36 | } |
Jordan Rose | 183ba8e | 2012-07-26 20:04:05 +0000 | [diff] [blame^] | 37 | |
| 38 | |
| 39 | void doSomething(); |
| 40 | void testSmartPointer2() { |
| 41 | char *mem = (char*)malloc(4); |
| 42 | { |
| 43 | SmartPointer Deleter(mem); |
| 44 | // Remove dead bindings... |
| 45 | doSomething(); |
| 46 | // destructor called here |
| 47 | } |
| 48 | *mem = 0; // expected-warning{{Use of memory after it is freed}} |
| 49 | } |
| 50 | |
| 51 | |
| 52 | class Subclass : public SmartPointer { |
| 53 | public: |
| 54 | Subclass(void *x) : SmartPointer(x) {} |
| 55 | }; |
| 56 | |
| 57 | void testSubclassSmartPointer() { |
| 58 | char *mem = (char*)malloc(4); |
| 59 | { |
| 60 | Subclass Deleter(mem); |
| 61 | // Remove dead bindings... |
| 62 | doSomething(); |
| 63 | // destructor called here |
| 64 | } |
| 65 | *mem = 0; // expected-warning{{Use of memory after it is freed}} |
| 66 | } |
| 67 | |
| 68 | |
| 69 | class MultipleInheritance : public Subclass, public SmartPointer { |
| 70 | public: |
| 71 | MultipleInheritance(void *a, void *b) : Subclass(a), SmartPointer(b) {} |
| 72 | }; |
| 73 | |
| 74 | void testMultipleInheritance1() { |
| 75 | char *mem = (char*)malloc(4); |
| 76 | { |
| 77 | MultipleInheritance Deleter(mem, 0); |
| 78 | // Remove dead bindings... |
| 79 | doSomething(); |
| 80 | // destructor called here |
| 81 | } |
| 82 | *mem = 0; // expected-warning{{Use of memory after it is freed}} |
| 83 | } |
| 84 | |
| 85 | void testMultipleInheritance2() { |
| 86 | char *mem = (char*)malloc(4); |
| 87 | { |
| 88 | MultipleInheritance Deleter(0, mem); |
| 89 | // Remove dead bindings... |
| 90 | doSomething(); |
| 91 | // destructor called here |
| 92 | } |
| 93 | *mem = 0; // expected-warning{{Use of memory after it is freed}} |
| 94 | } |
| 95 | |
| 96 | void testMultipleInheritance3() { |
| 97 | char *mem = (char*)malloc(4); |
| 98 | { |
| 99 | MultipleInheritance Deleter(mem, mem); |
| 100 | // Remove dead bindings... |
| 101 | doSomething(); |
| 102 | // destructor called here |
| 103 | // expected-warning@25 {{Attempt to free released memory}} |
| 104 | } |
| 105 | } |