Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
#include "clang/Analysis/FlowSensitive/RecordOps.h"
#include "clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h"
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
#include "clang/Analysis/FlowSensitive/Value.h"
#include "clang/Basic/LLVM.h"
Expand Down Expand Up @@ -842,6 +843,16 @@ transferNonConstMemberOperatorCall(const CXXOperatorCallExpr *Expr,
handleNonConstMemberCall(Expr, RecordLoc, Result, State);
}

static RecordStorageLocation *
getSmartPtrLikeStorageLocation(const Expr &E, const Environment &Env) {
if (!E.isPRValue())
return dyn_cast_or_null<RecordStorageLocation>(Env.getStorageLocation(E));
if (auto *PointerVal = dyn_cast_or_null<PointerValue>(Env.getValue(E)))
return dyn_cast_or_null<RecordStorageLocation>(
&PointerVal->getPointeeLoc());
return nullptr;
}

CFGMatchSwitch<LatticeTransferState>
buildTransferMatchSwitch(ASTContext &Ctx,
CFGMatchSwitchBuilder<LatticeTransferState> Builder) {
Expand Down Expand Up @@ -899,6 +910,43 @@ buildTransferMatchSwitch(ASTContext &Ctx,
transferLoggingGetReferenceableValueCall)
.CaseOfCFGStmt<CallExpr>(isLoggingCheckEqImpl(),
transferLoggingCheckEqImpl)
// This needs to go before the const accessor call matcher, because these
// look like them, but we model `operator`* and `get` to return the same
// object. Also, we model them for non-const cases.
.CaseOfCFGStmt<CXXOperatorCallExpr>(
isPointerLikeOperatorStar(),
[](const CXXOperatorCallExpr *E,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
transferSmartPointerLikeCachedDeref(
E, getSmartPtrLikeStorageLocation(*E->getArg(0), State.Env),
State, [](StorageLocation &Loc) {});
})
.CaseOfCFGStmt<CXXOperatorCallExpr>(
isPointerLikeOperatorArrow(),
[](const CXXOperatorCallExpr *E,
const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
transferSmartPointerLikeCachedGet(
E, getSmartPtrLikeStorageLocation(*E->getArg(0), State.Env),
State, [](StorageLocation &Loc) {});
})
.CaseOfCFGStmt<CXXMemberCallExpr>(
isSmartPointerLikeValueMethodCall(),
[](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
transferSmartPointerLikeCachedDeref(
E, getImplicitObjectLocation(*E, State.Env), State,
[](StorageLocation &Loc) {});
})
.CaseOfCFGStmt<CXXMemberCallExpr>(
isSmartPointerLikeGetMethodCall(),
[](const CXXMemberCallExpr *E, const MatchFinder::MatchResult &Result,
LatticeTransferState &State) {
transferSmartPointerLikeCachedGet(
E, getImplicitObjectLocation(*E, State.Env), State,
[](StorageLocation &Loc) {});
})
// const accessor calls
.CaseOfCFGStmt<CXXMemberCallExpr>(isConstStatusOrAccessorMemberCall(),
transferConstStatusOrAccessorMemberCall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,79 @@ TEST_P(UncheckedStatusOrAccessModelTest, AccessorCall) {
)cc");
}

TEST_P(UncheckedStatusOrAccessModelTest, PointerLike) {
ExpectDiagnosticsFor(R"cc(
#include "unchecked_statusor_access_test_defs.h"

class Foo {
public:
std::pair<int, STATUSOR_VOIDPTR>& operator*() const;
std::pair<int, STATUSOR_VOIDPTR>* operator->() const;
bool operator!=(const Foo& other) const;
};

void target() {
Foo foo;
if (foo->second.ok() && *foo->second != nullptr) {
*foo->second;
(*foo).second.value();
}
}
)cc");
ExpectDiagnosticsFor(R"cc(
#include "unchecked_statusor_access_test_defs.h"

class Foo {
public:
std::pair<int, STATUSOR_INT>& operator*() const;
std::pair<int, STATUSOR_INT>* operator->() const;
};
void target() {
Foo foo;
if (!foo->second.ok()) return;
foo->second.value();
(*foo).second.value();
}
)cc");
ExpectDiagnosticsFor(R"cc(
#include "unchecked_statusor_access_test_defs.h"

void target(std::pair<int, STATUSOR_VOIDPTR>* foo) {
if (foo->second.ok() && *foo->second != nullptr) {
*foo->second;
(*foo).second.value();
}
}
)cc");
}

TEST_P(UncheckedStatusOrAccessModelTest, UniquePtr) {
ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

void target() {
auto sor_up = Make<std::unique_ptr<STATUSOR_INT>>();
if (sor_up->ok()) sor_up->value();
}
)cc");
}

TEST_P(UncheckedStatusOrAccessModelTest, UniquePtrReset) {
ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

void target() {
auto sor_up = Make<std::unique_ptr<STATUSOR_INT>>();
if (sor_up->ok()) {
sor_up.reset(Make<STATUSOR_INT*>());
sor_up->value(); // [[unsafe]]
}
}
)cc");
}

} // namespace

std::string
Expand Down Expand Up @@ -3492,6 +3565,7 @@ GetHeaders(UncheckedStatusOrAccessModelTestAliasKind AliasKind) {
#include "std_pair.h"
#include "absl_log.h"
#include "testing_defs.h"
#include "std_unique_ptr.h"

template <typename T>
T Make();
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.