Jordan Rose | aea020f | 2013-01-26 01:28:23 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-null-return-paths=false -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -DSUPPRESSED=1 %s |
| 3 | |
Jordan Rose | aea020f | 2013-01-26 01:28:23 +0000 | [diff] [blame] | 4 | namespace rdar12676053 { |
| 5 | // Delta-reduced from a preprocessed file. |
| 6 | template<class T> |
| 7 | class RefCount { |
| 8 | T *ref; |
| 9 | public: |
| 10 | T *operator->() const { |
| 11 | return ref ? ref : 0; |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | class string {}; |
| 16 | |
| 17 | class ParserInputState { |
| 18 | public: |
| 19 | string filename; |
| 20 | }; |
| 21 | |
| 22 | class Parser { |
| 23 | void setFilename(const string& f) { |
| 24 | inputState->filename = f; |
| 25 | #ifndef SUPPRESSED |
| 26 | // expected-warning@-2 {{Called C++ object pointer is null}} |
| 27 | #endif |
| 28 | } |
| 29 | protected: |
| 30 | RefCount<ParserInputState> inputState; |
| 31 | }; |
Jordan Rose | 801916b | 2013-03-01 19:45:10 +0000 | [diff] [blame^] | 32 | } |
| 33 | |
| 34 | |
| 35 | // This is the standard placement new. |
| 36 | inline void* operator new(__typeof__(sizeof(int)), void* __p) throw() |
| 37 | { |
| 38 | return __p; |
| 39 | } |
| 40 | |
| 41 | extern bool coin(); |
| 42 | |
| 43 | namespace References { |
| 44 | class Map { |
| 45 | int *&getNewBox(); |
| 46 | int *firstBox; |
| 47 | |
| 48 | public: |
| 49 | int *&getValue(int key) { |
| 50 | if (coin()) { |
| 51 | return firstBox; |
| 52 | } else { |
| 53 | int *&newBox = getNewBox(); |
| 54 | newBox = 0; |
| 55 | return newBox; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int *&getValueIndirectly(int key) { |
| 60 | int *&valueBox = getValue(key); |
| 61 | return valueBox; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | void testMap(Map &m, int i) { |
| 66 | *m.getValue(i) = 1; |
| 67 | #ifndef SUPPRESSED |
| 68 | // expected-warning@-2 {{Dereference of null pointer}} |
| 69 | #endif |
| 70 | |
| 71 | *m.getValueIndirectly(i) = 1; |
| 72 | #ifndef SUPPRESSED |
| 73 | // expected-warning@-2 {{Dereference of null pointer}} |
| 74 | #endif |
| 75 | |
| 76 | int *&box = m.getValue(i); |
| 77 | extern int *getPointer(); |
| 78 | box = getPointer(); |
| 79 | *box = 1; // no-warning |
| 80 | |
| 81 | int *&box2 = m.getValue(i); |
| 82 | box = 0; |
| 83 | *box = 1; // expected-warning {{Dereference of null pointer}} |
| 84 | } |
| 85 | |
| 86 | class SomeClass { |
| 87 | public: |
| 88 | void doSomething(); |
| 89 | }; |
| 90 | |
| 91 | SomeClass *&getSomeClass() { |
| 92 | if (coin()) { |
| 93 | extern SomeClass *&opaqueClass(); |
| 94 | return opaqueClass(); |
| 95 | } else { |
| 96 | static SomeClass *sharedClass; |
| 97 | sharedClass = 0; |
| 98 | return sharedClass; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void testClass() { |
| 103 | getSomeClass()->doSomething(); |
| 104 | #ifndef SUPPRESSED |
| 105 | // expected-warning@-2 {{Called C++ object pointer is null}} |
| 106 | #endif |
| 107 | |
| 108 | // Separate the lvalue-to-rvalue conversion from the subsequent dereference. |
| 109 | SomeClass *object = getSomeClass(); |
| 110 | object->doSomething(); |
| 111 | #ifndef SUPPRESSED |
| 112 | // expected-warning@-2 {{Called C++ object pointer is null}} |
| 113 | #endif |
| 114 | } |
| 115 | } |