-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[FlowSensitive] [StatusOr] [15/N] Support nested StatusOrs #170950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fmayer
wants to merge
1
commit into
users/fmayer/spr/main.flowsensitive-statusor-15n-support-nested-statusors
Choose a base branch
from
users/fmayer/spr/flowsensitive-statusor-15n-support-nested-statusors
base: users/fmayer/spr/main.flowsensitive-statusor-15n-support-nested-statusors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created using spr 1.3.7
Member
|
@llvm/pr-subscribers-clang Author: Florian Mayer (fmayer) ChangesFull diff: https://github.com/llvm/llvm-project/pull/170950.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
index 1b68d704239e8..c917c8e8c11ba 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
@@ -1037,6 +1037,26 @@ transferAssertionResultOperatorBoolCall(const CXXMemberCallExpr *Expr,
State.Env.setValue(*Expr, Res);
}
+static void transferDerefCall(const CallExpr *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 CallExpr *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")));
+}
+
static RecordStorageLocation *
getSmartPtrLikeStorageLocation(const Expr &E, const Environment &Env) {
if (!E.isPRValue())
@@ -1123,6 +1143,10 @@ buildTransferMatchSwitch(ASTContext &Ctx,
transferValueAssignmentCall)
.CaseOfCFGStmt<CXXConstructExpr>(isStatusOrValueConstructor(),
transferValueConstructor)
+ .CaseOfCFGStmt<CallExpr>(isStatusOrOperatorCallWithName("->"),
+ transferArrowCall)
+ .CaseOfCFGStmt<CallExpr>(isStatusOrOperatorCallWithName("*"),
+ transferDerefCall)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatus(),
transferAsStatusCallWithStatus)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatusOr(),
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
index 48e61abf09f19..cd7353c62f537 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
@@ -3691,6 +3691,155 @@ 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).sor.ok()) (*foo).sor.value();
+ }
+ )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");
+
+ 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");
+
+ 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()) 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
|
Member
|
@llvm/pr-subscribers-clang-analysis Author: Florian Mayer (fmayer) ChangesFull diff: https://github.com/llvm/llvm-project/pull/170950.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
index 1b68d704239e8..c917c8e8c11ba 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp
@@ -1037,6 +1037,26 @@ transferAssertionResultOperatorBoolCall(const CXXMemberCallExpr *Expr,
State.Env.setValue(*Expr, Res);
}
+static void transferDerefCall(const CallExpr *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 CallExpr *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")));
+}
+
static RecordStorageLocation *
getSmartPtrLikeStorageLocation(const Expr &E, const Environment &Env) {
if (!E.isPRValue())
@@ -1123,6 +1143,10 @@ buildTransferMatchSwitch(ASTContext &Ctx,
transferValueAssignmentCall)
.CaseOfCFGStmt<CXXConstructExpr>(isStatusOrValueConstructor(),
transferValueConstructor)
+ .CaseOfCFGStmt<CallExpr>(isStatusOrOperatorCallWithName("->"),
+ transferArrowCall)
+ .CaseOfCFGStmt<CallExpr>(isStatusOrOperatorCallWithName("*"),
+ transferDerefCall)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatus(),
transferAsStatusCallWithStatus)
.CaseOfCFGStmt<CallExpr>(isAsStatusCallWithStatusOr(),
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
index 48e61abf09f19..cd7353c62f537 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp
@@ -3691,6 +3691,155 @@ 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).sor.ok()) (*foo).sor.value();
+ }
+ )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");
+
+ 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");
+
+ 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()) 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
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:analysis
clang:dataflow
Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.