1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// RUN: %clang_analyze_cc1\
// RUN: -analyzer-checker=core,cplusplus.Move,alpha.cplusplus.SmartPtr,debug.ExprInspection\
// RUN: -analyzer-config cplusplus.SmartPtrModeling:ModelSmartPtrDereference=true\
// RUN: -analyzer-output=text -std=c++20 %s -verify=expected
// RUN: %clang_analyze_cc1\
// RUN: -analyzer-checker=core,cplusplus.Move,alpha.cplusplus.SmartPtr,debug.ExprInspection\
// RUN: -analyzer-config cplusplus.SmartPtrModeling:ModelSmartPtrDereference=true\
// RUN: -analyzer-output=text -std=c++11 %s -verify=expected
#include "Inputs/system-header-simulator-cxx.h"
void clang_analyzer_eval(bool);
class A {
public:
A(){};
void foo();
};
A *return_null() {
return nullptr;
}
void derefAfterDefaultCtr() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefAfterCtrWithNull() {
A *NullInnerPtr = nullptr; // expected-note {{'NullInnerPtr' initialized to a null pointer value}}
std::unique_ptr<A> P(NullInnerPtr); // expected-note {{Smart pointer 'P' is constructed using a null value}}
*P; // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefAfterCtrWithNullVariable() {
A *NullInnerPtr = nullptr; // expected-note {{'NullInnerPtr' initialized to a null pointer value}}
std::unique_ptr<A> P(NullInnerPtr); // expected-note {{Smart pointer 'P' is constructed using a null value}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefAfterRelease() {
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
// FIXME: should mark region as uninteresting after release, so above note will not be there
P.release(); // expected-note {{Smart pointer 'P' is released and set to null}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefAfterReset() {
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
P.reset(); // expected-note {{Smart pointer 'P' reset using a null value}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefAfterResetWithNull() {
A *NullInnerPtr = nullptr; // expected-note {{'NullInnerPtr' initialized to a null pointer value}}
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
P.reset(NullInnerPtr); // expected-note {{Smart pointer 'P' reset using a null value}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
// FIXME: Fix this test when support is added for tracking raw pointer
// and mark the smart pointer as interesting based on that and add tags.
void derefOnReleasedNullRawPtr() {
std::unique_ptr<A> P; // FIXME: add note "Default constructed smart pointer 'P' is null"
A *AP = P.release(); // expected-note {{'AP' initialized to a null pointer value}}
AP->foo(); // expected-warning {{Called C++ object pointer is null [core.CallAndMessage]}}
// expected-note@-1{{Called C++ object pointer is null}}
}
void derefOnSwappedNullPtr() {
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
std::unique_ptr<A> PNull;
P.swap(PNull);
PNull->foo(); // No warning.
(*P).foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefOnStdSwappedNullPtr() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
std::unique_ptr<A> PNull;
std::swap(P, PNull);
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
struct StructWithSmartPtr { // expected-note {{Default constructed smart pointer 'S.P' is null}}
std::unique_ptr<A> P;
};
void derefAfterDefaultCtrInsideStruct() {
StructWithSmartPtr S; // expected-note {{Calling implicit default constructor for 'StructWithSmartPtr'}}
// expected-note@-1 {{Returning from default constructor for 'StructWithSmartPtr'}}
S.P->foo(); // expected-warning {{Dereference of null smart pointer 'S.P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'S.P'}}
}
void noNoteTagsForNonInterestingRegion() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
std::unique_ptr<A> P1; // No note.
std::unique_ptr<A> P2; // No note.
P1.release(); // No note.
P1.reset(); // No note.
P1.swap(P2); // No note.
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefOnRawPtrFromGetOnNullPtr() {
std::unique_ptr<A> P; // FIXME: add note "Default constructed smart pointer 'P' is null"
P.get()->foo(); // expected-warning {{Called C++ object pointer is null [core.CallAndMessage]}}
// expected-note@-1 {{Called C++ object pointer is null}}
}
void derefOnRawPtrFromGetOnValidPtr() {
std::unique_ptr<A> P(new A());
P.get()->foo(); // No warning.
}
void derefOnRawPtrFromGetOnUnknownPtr(std::unique_ptr<A> P) {
P.get()->foo(); // No warning.
}
void derefOnMovedFromValidPtr() {
std::unique_ptr<A> PToMove(new A()); // expected-note {{Smart pointer 'PToMove' is constructed}}
// FIXME: above note should go away once we fix marking region not interested.
std::unique_ptr<A> P;
P = std::move(PToMove); // expected-note {{Smart pointer 'PToMove' is null after being moved to 'P'}}
PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1 {{Dereference of null smart pointer 'PToMove'}}
}
void derefOnMovedToNullPtr() {
std::unique_ptr<A> PToMove(new A());
std::unique_ptr<A> P;
P = std::move(PToMove); // No note.
P->foo(); // No warning.
}
void derefOnNullPtrGotMovedFromValidPtr() {
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
// FIXME: above note should go away once we fix marking region not interested.
std::unique_ptr<A> PToMove; // expected-note {{Default constructed smart pointer 'PToMove' is null}}
P = std::move(PToMove); // expected-note {{A null pointer value is moved to 'P'}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1 {{Dereference of null smart pointer 'P'}}
}
void derefOnMovedUnknownPtr(std::unique_ptr<A> PToMove) {
std::unique_ptr<A> P;
P = std::move(PToMove); // expected-note {{Smart pointer 'PToMove' is null after; previous value moved to 'P'}}
PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1 {{Dereference of null smart pointer 'PToMove'}}
}
void derefOnAssignedNullPtrToNullSmartPtr() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
P = nullptr; // expected-note {{Smart pointer 'P' is assigned to null}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1 {{Dereference of null smart pointer 'P'}}
}
void derefOnAssignedZeroToNullSmartPtr() {
std::unique_ptr<A> P(new A()); // expected-note {{Smart pointer 'P' is constructed}}
// FIXME: above note should go away once we fix marking region not interested.
P = 0; // expected-note {{Smart pointer 'P' is assigned to null}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1 {{Dereference of null smart pointer 'P'}}
}
void derefMoveConstructedWithNullPtr() {
std::unique_ptr<A> PToMove; // expected-note {{Default constructed smart pointer 'PToMove' is null}}
std::unique_ptr<A> P(std::move(PToMove)); // expected-note {{A null pointer value is moved to 'P'}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
void derefValidPtrMovedToConstruct() {
std::unique_ptr<A> PToMove(new A()); // expected-note {{Smart pointer 'PToMove' is constructed}}
// FIXME: above note should go away once we fix marking region not interested.
std::unique_ptr<A> P(std::move(PToMove)); // expected-note {{Smart pointer 'PToMove' is null after being moved to 'P'}}
PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PToMove'}}
}
void derefNullPtrMovedToConstruct() {
std::unique_ptr<A> PToMove; // expected-note {{Default constructed smart pointer 'PToMove' is null}}
// FIXME: above note should go away once we fix marking region not interested.
std::unique_ptr<A> P(std::move(PToMove)); // expected-note {{Smart pointer 'PToMove' is null after being moved to 'P'}}
PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PToMove'}}
}
void derefUnknownPtrMovedToConstruct(std::unique_ptr<A> PToMove) {
std::unique_ptr<A> P(std::move(PToMove)); // expected-note {{Smart pointer 'PToMove' is null after; previous value moved to 'P'}}
PToMove->foo(); // expected-warning {{Dereference of null smart pointer 'PToMove' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PToMove'}}
}
void derefConditionOnNullPtrFalseBranch() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
if (P) { // expected-note {{Taking false branch}}
P->foo(); // No warning.
} else {
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
}
void derefConditionOnNullPtrTrueBranch() {
std::unique_ptr<A> P; // expected-note {{Default constructed smart pointer 'P' is null}}
if (!P) { // expected-note {{Taking true branch}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
}
void derefConditionOnValidPtrTrueBranch() {
std::unique_ptr<A> P(new A());
std::unique_ptr<A> PNull; // expected-note {{Default constructed smart pointer 'PNull' is null}}
if (P) { // expected-note {{Taking true branch}}
PNull->foo(); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PNull'}}
} else {
PNull->foo(); // No warning
}
}
void derefConditionOnValidPtrFalseBranch() {
std::unique_ptr<A> P(new A());
std::unique_ptr<A> PNull; // expected-note {{Default constructed smart pointer 'PNull' is null}}
if (!P) { // expected-note {{Taking false branch}}
PNull->foo(); // No warning
} else {
PNull->foo(); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PNull'}}
}
}
void derefConditionOnNotValidPtr() {
std::unique_ptr<A> P(new A());
std::unique_ptr<A> PNull;
if (!P)
PNull->foo(); // No warning.
}
void derefConditionOnUnKnownPtrAssumeNull(std::unique_ptr<A> P) {
std::unique_ptr<A> PNull; // expected-note {{Default constructed smart pointer 'PNull' is null}}
if (!P) { // expected-note {{Taking true branch}}
// expected-note@-1{{Assuming smart pointer 'P' is null}}
PNull->foo(); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PNull'}}
}
}
void derefConditionOnUnKnownPtrAssumeNonNull(std::unique_ptr<A> P) {
std::unique_ptr<A> PNull; // expected-note {{Default constructed smart pointer 'PNull' is null}}
if (P) { // expected-note {{Taking true branch}}
// expected-note@-1{{Assuming smart pointer 'P' is non-null}}
PNull->foo(); // expected-warning {{Dereference of null smart pointer 'PNull' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'PNull'}}
}
}
void derefOnValidPtrAfterReset(std::unique_ptr<A> P) {
P.reset(new A());
if (!P)
P->foo(); // No warning.
else
P->foo(); // No warning.
}
struct S {
std::unique_ptr<int> P;
void foo() {
if (!P) { // No-note because foo() is pruned
return;
}
}
int callingFooWithNullPointer() {
foo(); // No note on Calling 'S::foo'
P.reset(new int(0)); // expected-note {{Assigning 0}}
return 1 / *(P.get()); // expected-warning {{Division by zero [core.DivideZero]}}
// expected-note@-1 {{Division by zero}}
}
int callingFooWithValidPointer() {
P.reset(new int(0)); // expected-note {{Assigning 0}}
foo(); // No note on Calling 'S::foo'
return 1 / *(P.get()); // expected-warning {{Division by zero [core.DivideZero]}}
// expected-note@-1 {{Division by zero}}
}
int callingFooWithUnknownPointer(std::unique_ptr<int> PUnknown) {
P.swap(PUnknown);
foo(); // No note on Calling 'S::foo'
P.reset(new int(0)); // expected-note {{Assigning 0}}
return 1 / *(P.get()); // expected-warning {{Division by zero [core.DivideZero]}}
// expected-note@-1 {{Division by zero}}
}
};
void derefAfterBranchingOnUnknownInnerPtr(std::unique_ptr<A> P) {
A *RP = P.get();
if (!RP) { // expected-note {{Assuming 'RP' is null}}
// expected-note@-1 {{Taking true branch}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
}
void makeUniqueReturnsNonNullUniquePtr() {
auto P = std::make_unique<A>();
if (!P) { // expected-note {{Taking false branch}}
P->foo(); // should have no warning here, path is impossible
}
P.reset(); // expected-note {{Smart pointer 'P' reset using a null value}}
// Now P is null
if (!P) {
// expected-note@-1 {{Taking true branch}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
}
#if __cplusplus >= 202002L
void makeUniqueForOverwriteReturnsNullUniquePtr() {
auto P = std::make_unique_for_overwrite<A>();
if (!P) { // expected-note {{Taking false branch}}
P->foo(); // should have no warning here, path is impossible
}
P.reset(); // expected-note {{Smart pointer 'P' reset using a null value}}
// Now P is null
if (!P) {
// expected-note@-1 {{Taking true branch}}
P->foo(); // expected-warning {{Dereference of null smart pointer 'P' [alpha.cplusplus.SmartPtr]}}
// expected-note@-1{{Dereference of null smart pointer 'P'}}
}
}
#endif
struct G {
int *p;
G(int *p): p(p) {}
~G() { *p = 0; }
};
void foo() {
int x = 1;
{
auto P = std::make_unique<G>(&x);
// FIXME: There should not be a state split here, it should take the true path.
clang_analyzer_eval(*P->p == 1); // expected-warning {{TRUE}}
// expected-warning@-1 {{FALSE}}
// expected-note@-2 {{Assuming the condition is true}}
// expected-note@-3 {{Assuming the condition is false}}
// expected-note@-4 {{TRUE}}
// expected-note@-5 {{FALSE}}
// expected-note@-6 {{Assuming the condition is false}}
}
// FIXME: Should be fixed when unique_ptr desctructors are
// properly modelled. This includes modelling the call to
// the destructor of the inner pointer type.
clang_analyzer_eval(x == 0); // expected-warning {{FALSE}}
// expected-note@-1 {{FALSE}}
}
|