Skip to content
Merged
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 @@ -1044,6 +1044,36 @@ transferAssertionResultOperatorBoolCall(const CXXMemberCallExpr *Expr,
State.Env.setValue(*Expr, Res);
}

static void transferDerefCall(const CXXOperatorCallExpr *Expr,
const MatchFinder::MatchResult &,
LatticeTransferState &State) {
auto *StatusOrLoc = State.Env.get<RecordStorageLocation>(*Expr->getArg(0));

if (StatusOrLoc && State.Env.getStorageLocation(*Expr) == nullptr)
State.Env.setStorageLocation(*Expr,
StatusOrLoc->getSyntheticField("value"));
}

static void transferArrowCall(const CXXOperatorCallExpr *Expr,
const MatchFinder::MatchResult &,
LatticeTransferState &State) {
auto *StatusOrLoc = State.Env.get<RecordStorageLocation>(*Expr->getArg(0));
if (!StatusOrLoc)
return;
State.Env.setValue(*Expr, State.Env.create<PointerValue>(
StatusOrLoc->getSyntheticField("value")));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are probably most common for nesting, but could it be possible to have a foo.value().value() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


static void transferValueCall(const CXXMemberCallExpr *Expr,
const MatchFinder::MatchResult &,
LatticeTransferState &State) {
auto *StatusOrLoc = getImplicitObjectLocation(*Expr, State.Env);

if (StatusOrLoc && State.Env.getStorageLocation(*Expr) == nullptr)
State.Env.setStorageLocation(*Expr,
StatusOrLoc->getSyntheticField("value"));
}

static RecordStorageLocation *
getSmartPtrLikeStorageLocation(const Expr &E, const Environment &Env) {
if (!E.isPRValue())
Expand Down Expand Up @@ -1130,6 +1160,12 @@ buildTransferMatchSwitch(ASTContext &Ctx,
transferValueAssignmentCall)
.CaseOfCFGStmt<CXXConstructExpr>(isStatusOrValueConstructor(),
transferValueConstructor)
.CaseOfCFGStmt<CXXOperatorCallExpr>(isStatusOrOperatorCallWithName("->"),
transferArrowCall)
.CaseOfCFGStmt<CXXOperatorCallExpr>(isStatusOrOperatorCallWithName("*"),
transferDerefCall)
.CaseOfCFGStmt<CXXMemberCallExpr>(isStatusOrMemberCallWithName("value"),
transferValueCall)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatus(),
transferAsStatusCallWithStatus)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatusOr(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,15 @@ TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOr) {
result.value();
}
)cc");
ExpectDiagnosticsFor(R"cc(
#include "unchecked_statusor_access_test_defs.h"

void target() {
absl::StatusOr<STATUSOR_INT> result = Make<STATUSOR_INT>();
if (result.value().ok())
result.value().value();
}
)cc");
}

TEST_P(UncheckedStatusOrAccessModelTest, PtrConstruct) {
Expand Down Expand Up @@ -3739,6 +3748,168 @@ TEST_P(UncheckedStatusOrAccessModelTest, UniquePtrReset) {
)cc");
}

TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOrInStatusOrStruct) {
// Non-standard assignment with a nested StatusOr.
ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Inner {
absl::StatusOr<std::string> sor;
};

struct Outer {
absl::StatusOr<Inner> inner;
};

void target() {
Outer foo = Make<Outer>();
foo.inner->sor = "a"; // [[unsafe]]
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(const absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo->sor.ok()) foo->sor.value();
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(const absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo.value().sor.ok()) foo->sor.value();
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(const absl::StatusOr<Foo>& foo) {
if (foo.ok() && (*foo).sor.ok()) (*foo).sor.value();
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo->sor.ok()) *foo->sor;
}
)cc");
// With assignment.
ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo->sor.ok()) {
foo->sor = Make<absl::StatusOr<std::string>>();
foo->sor.value(); // [[unsafe]]
}
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo->sor.ok()) {
auto n = Make<absl::StatusOr<std::string>>();
if (n.ok()) foo->sor = n;
foo->sor.value();
}
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo) {
if (foo.ok() && foo->sor.ok()) {
auto n = Make<absl::StatusOr<std::string>>();
if (n.ok()) foo->sor = std::move(n);
foo->sor.value();
}
}
)cc");

// More complicated conditionals.
ExpectDiagnosticsFor(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment to split this from the // With assignment. -- starting a new section for say more complex conditionals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo) {
if (!foo.ok()) return;
if (!foo->sor.ok())
foo->sor.value(); // [[unsafe]]
else
foo->sor.value();
}
)cc");

ExpectDiagnosticsFor(
R"cc(
#include "unchecked_statusor_access_test_defs.h"

struct Foo {
absl::StatusOr<std::string> sor;
};

void target(absl::StatusOr<Foo>& foo, bool b) {
if (!foo.ok()) return;
if (b) {
if (!foo->sor.ok()) return;
foo->sor.value();
} else {
if (!foo->sor.ok()) return;
foo->sor.value();
}
foo->sor.value();
}
)cc");
}

} // namespace

std::string
Expand Down
Loading