1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection\ 2 // RUN: -analyzer-checker cplusplus.Move,alpha.cplusplus.SmartPtr\ 3 // RUN: -analyzer-config cplusplus.SmartPtrModeling:ModelSmartPtrDereference=true\ 4 // RUN: -std=c++11 -verify %s 5 6 #include "Inputs/system-header-simulator-cxx.h" 7 8 void clang_analyzer_warnIfReached(); 9 void clang_analyzer_numTimesReached(); 10 void clang_analyzer_eval(bool); 11 12 void derefAfterMove(std::unique_ptr<int> P) { 13 std::unique_ptr<int> Q = std::move(P); 14 if (Q) 15 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}} 16 *Q.get() = 1; // no-warning 17 if (P) 18 clang_analyzer_warnIfReached(); // no-warning 19 // TODO: Report a null dereference (instead). 20 *P.get() = 1; // expected-warning {{Method called on moved-from object 'P'}} 21 } 22 23 // Don't crash when attempting to model a call with unknown callee. 24 namespace testUnknownCallee { 25 struct S { 26 void foo(); 27 }; 28 void bar(S *s, void (S::*func)(void)) { 29 (s->*func)(); // no-crash 30 } 31 } // namespace testUnknownCallee 32 33 class A { 34 public: 35 A(){}; 36 void foo(); 37 }; 38 39 A *return_null() { 40 return nullptr; 41 } 42 43 void derefAfterValidCtr() { 44 std::unique_ptr<A> P(new A()); 45 clang_analyzer_numTimesReached(); // expected-warning {{1}} 46 P->foo(); // No warning. 47 } 48 49 void derefOfUnknown(std::unique_ptr<A> P) { 50 P->foo(); // No warning. 51 } 52 53 void derefAfterDefaultCtr() { 54 std::unique_ptr<A> P; 55 clang_analyzer_numTimesReached(); // expected-warning {{1}} 56 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 57 } 58 59 void derefAfterCtrWithNull() { 60 std::unique_ptr<A> P(nullptr); 61 clang_analyzer_numTimesReached(); // expected-warning {{1}} 62 *P; // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 63 } 64 65 void derefAfterCtrWithNullVariable() { 66 A *InnerPtr = nullptr; 67 std::unique_ptr<A> P(InnerPtr); 68 clang_analyzer_numTimesReached(); // expected-warning {{1}} 69 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 70 } 71 72 void derefAfterRelease() { 73 std::unique_ptr<A> P(new A()); 74 P.release(); 75 clang_analyzer_numTimesReached(); // expected-warning {{1}} 76 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 77 } 78 79 void derefAfterReset() { 80 std::unique_ptr<A> P(new A()); 81 P.reset(); 82 clang_analyzer_numTimesReached(); // expected-warning {{1}} 83 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 84 } 85 86 void derefAfterResetWithNull() { 87 std::unique_ptr<A> P(new A()); 88 P.reset(nullptr); 89 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 90 } 91 92 void derefAfterResetWithNonNull() { 93 std::unique_ptr<A> P; 94 P.reset(new A()); 95 clang_analyzer_numTimesReached(); // expected-warning {{1}} 96 P->foo(); // No warning. 97 } 98 99 void derefAfterReleaseAndResetWithNonNull() { 100 std::unique_ptr<A> P(new A()); 101 P.release(); 102 P.reset(new A()); 103 P->foo(); // No warning. 104 } 105 106 void derefOnReleasedNullRawPtr() { 107 std::unique_ptr<A> P; 108 A *AP = P.release(); 109 AP->foo(); // expected-warning {{Called C++ object pointer is null [core.CallAndMessage]}} 110 } 111 112 void derefOnReleasedValidRawPtr() { 113 std::unique_ptr<A> P(new A()); 114 A *AP = P.release(); 115 AP->foo(); // No warning. 116 } 117 118 void pass_smart_ptr_by_ref(std::unique_ptr<A> &a); 119 void pass_smart_ptr_by_const_ref(const std::unique_ptr<A> &a); 120 void pass_smart_ptr_by_rvalue_ref(std::unique_ptr<A> &&a); 121 void pass_smart_ptr_by_const_rvalue_ref(const std::unique_ptr<A> &&a); 122 void pass_smart_ptr_by_ptr(std::unique_ptr<A> *a); 123 void pass_smart_ptr_by_const_ptr(const std::unique_ptr<A> *a); 124 125 void regioninvalidationWithPassByRef() { 126 std::unique_ptr<A> P; 127 pass_smart_ptr_by_ref(P); 128 P->foo(); // no-warning 129 } 130 131 void regioninvalidationWithPassByCostRef() { 132 std::unique_ptr<A> P; 133 pass_smart_ptr_by_const_ref(P); 134 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 135 } 136 137 void regioninvalidationWithPassByRValueRef() { 138 std::unique_ptr<A> P; 139 pass_smart_ptr_by_rvalue_ref(std::move(P)); 140 P->foo(); // no-warning 141 } 142 143 void regioninvalidationWithPassByConstRValueRef() { 144 std::unique_ptr<A> P; 145 pass_smart_ptr_by_const_rvalue_ref(std::move(P)); 146 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 147 } 148 149 void regioninvalidationWithPassByPtr() { 150 std::unique_ptr<A> P; 151 pass_smart_ptr_by_ptr(&P); 152 P->foo(); 153 } 154 155 void regioninvalidationWithPassByConstPtr() { 156 std::unique_ptr<A> P; 157 pass_smart_ptr_by_const_ptr(&P); 158 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 159 } 160 161 struct StructWithSmartPtr { 162 std::unique_ptr<A> P; 163 }; 164 165 void pass_struct_with_smart_ptr_by_ref(StructWithSmartPtr &a); 166 void pass_struct_with_smart_ptr_by_const_ref(const StructWithSmartPtr &a); 167 void pass_struct_with_smart_ptr_by_rvalue_ref(StructWithSmartPtr &&a); 168 void pass_struct_with_smart_ptr_by_const_rvalue_ref(const StructWithSmartPtr &&a); 169 void pass_struct_with_smart_ptr_by_ptr(StructWithSmartPtr *a); 170 void pass_struct_with_smart_ptr_by_const_ptr(const StructWithSmartPtr *a); 171 172 void regioninvalidationWithinStructPassByRef() { 173 StructWithSmartPtr S; 174 pass_struct_with_smart_ptr_by_ref(S); 175 S.P->foo(); // no-warning 176 } 177 178 void regioninvalidationWithinStructPassByConstRef() { 179 StructWithSmartPtr S; 180 pass_struct_with_smart_ptr_by_const_ref(S); 181 S.P->foo(); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}} 182 } 183 184 void regioninvalidationWithinStructPassByRValueRef() { 185 StructWithSmartPtr S; 186 pass_struct_with_smart_ptr_by_rvalue_ref(std::move(S)); 187 S.P->foo(); // no-warning 188 } 189 190 void regioninvalidationWithinStructPassByConstRValueRef() { 191 StructWithSmartPtr S; 192 pass_struct_with_smart_ptr_by_const_rvalue_ref(std::move(S)); 193 S.P->foo(); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}} 194 } 195 196 void regioninvalidationWithinStructPassByPtr() { 197 StructWithSmartPtr S; 198 pass_struct_with_smart_ptr_by_ptr(&S); 199 S.P->foo(); // no-warning 200 } 201 202 void regioninvalidationWithinStructPassByConstPtr() { 203 StructWithSmartPtr S; 204 pass_struct_with_smart_ptr_by_const_ptr(&S); 205 S.P->foo(); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}} 206 } 207 208 void derefAfterAssignment() { 209 { 210 std::unique_ptr<A> P(new A()); 211 std::unique_ptr<A> Q; 212 Q = std::move(P); 213 Q->foo(); // no-warning 214 } 215 { 216 std::unique_ptr<A> P; 217 std::unique_ptr<A> Q; 218 Q = std::move(P); 219 Q->foo(); // expected-warning {{Dereference of null smart pointer 'Q' [alpha.cplusplus.SmartPtr]}} 220 } 221 } 222 223 void derefOnSwappedNullPtr() { 224 std::unique_ptr<A> P(new A()); 225 std::unique_ptr<A> PNull; 226 P.swap(PNull); 227 PNull->foo(); // No warning. 228 (*P).foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 229 } 230 231 void derefOnFirstStdSwappedNullPtr() { 232 std::unique_ptr<A> P; 233 std::unique_ptr<A> PNull; 234 std::swap(P, PNull); 235 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 236 } 237 238 void derefOnSecondStdSwappedNullPtr() { 239 std::unique_ptr<A> P; 240 std::unique_ptr<A> PNull; 241 std::swap(P, PNull); 242 PNull->foo(); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}} 243 } 244 245 void derefOnSwappedValidPtr() { 246 std::unique_ptr<A> P(new A()); 247 std::unique_ptr<A> PValid(new A()); 248 P.swap(PValid); 249 (*P).foo(); // No warning. 250 PValid->foo(); // No warning. 251 std::swap(P, PValid); 252 P->foo(); // No warning. 253 PValid->foo(); // No warning. 254 } 255 256 void derefOnRawPtrFromGetOnNullPtr() { 257 std::unique_ptr<A> P; 258 P.get()->foo(); // expected-warning {{Called C++ object pointer is null [core.CallAndMessage]}} 259 } 260 261 void derefOnRawPtrFromGetOnValidPtr() { 262 std::unique_ptr<A> P(new A()); 263 P.get()->foo(); // No warning. 264 } 265 266 void derefOnRawPtrFromGetOnUnknownPtr(std::unique_ptr<A> P) { 267 P.get()->foo(); // No warning. 268 } 269 270 void derefOnRawPtrFromMultipleGetOnUnknownPtr(std::unique_ptr<A> P) { 271 A *X = P.get(); 272 A *Y = P.get(); 273 clang_analyzer_eval(X == Y); // expected-warning{{TRUE}} 274 if (!X) { 275 Y->foo(); // expected-warning {{Called C++ object pointer is null [core.CallAndMessage]}} 276 } 277 } 278 279 void derefOnMovedFromValidPtr() { 280 std::unique_ptr<A> PToMove(new A()); 281 std::unique_ptr<A> P; 282 P = std::move(PToMove); 283 PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}} 284 } 285 286 void derefOnMovedToNullPtr() { 287 std::unique_ptr<A> PToMove(new A()); 288 std::unique_ptr<A> P; 289 P = std::move(PToMove); // No note. 290 P->foo(); // No warning. 291 } 292 293 void derefOnNullPtrGotMovedFromValidPtr() { 294 std::unique_ptr<A> P(new A()); 295 std::unique_ptr<A> PToMove; 296 P = std::move(PToMove); 297 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 298 } 299 300 void derefOnMovedFromUnknownPtr(std::unique_ptr<A> PToMove) { 301 std::unique_ptr<A> P; 302 P = std::move(PToMove); 303 P->foo(); // No warning. 304 } 305 306 void derefOnMovedUnknownPtr(std::unique_ptr<A> PToMove) { 307 std::unique_ptr<A> P; 308 P = std::move(PToMove); 309 PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}} 310 } 311 312 void derefOnAssignedNullPtrToNullSmartPtr() { 313 std::unique_ptr<A> P; 314 P = nullptr; 315 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 316 } 317 318 void derefOnAssignedZeroToNullSmartPtr() { 319 std::unique_ptr<A> P(new A()); 320 P = 0; 321 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 322 } 323 324 void derefOnAssignedNullToUnknowSmartPtr(std::unique_ptr<A> P) { 325 P = nullptr; 326 P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}} 327 } 328 329 std::unique_ptr<A> &&returnRValRefOfUniquePtr(); 330 331 void drefOnAssignedNullFromMethodPtrValidSmartPtr() { 332 std::unique_ptr<A> P(new A()); 333 P = returnRValRefOfUniquePtr(); 334 P->foo(); // No warning. 335 } 336