Skip to content
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

server: Return nullopt when process_context is nullptr #14181

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions source/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ void InstanceImpl::flushStatsInternal() {

bool InstanceImpl::healthCheckFailed() { return !live_.load(); }

ProcessContextOptRef InstanceImpl::processContext() {
if (process_context_ == nullptr) {
return absl::nullopt;
} else {
justin-mp marked this conversation as resolved.
Show resolved Hide resolved
return *process_context_;
}
}

namespace {
// Loads a bootstrap object, potentially at a specific version (upgrading if necessary).
void loadBootstrap(absl::optional<uint32_t> bootstrap_version,
Expand Down
2 changes: 1 addition & 1 deletion source/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class InstanceImpl final : Logger::Loggable<Logger::Id::main>,
Stats::Store& stats() override { return stats_store_; }
Grpc::Context& grpcContext() override { return grpc_context_; }
Http::Context& httpContext() override { return http_context_; }
ProcessContextOptRef processContext() override { return *process_context_; }
ProcessContextOptRef processContext() override;
ThreadLocal::Instance& threadLocal() override { return thread_local_; }
const LocalInfo::LocalInfo& localInfo() const override { return *local_info_; }
TimeSource& timeSource() override { return time_source_; }
Expand Down
22 changes: 22 additions & 0 deletions test/server/server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,28 @@ TEST_P(ServerInstanceImplTest, DisabledExtension) {
ASSERT_TRUE(disabled_filter_found);
}

TEST_P(ServerInstanceImplTest, NullProcessContextTest) {
// These are already the defaults. Repeated here for clarity.
process_object_ = nullptr;
process_context_ = nullptr;

initialize("test/server/test_data/server/empty_bootstrap.yaml");
ProcessContextOptRef context = server_->processContext();
EXPECT_FALSE(context.has_value());

// Prior to the commit when this test was added, the code would return a
// context where has_value() was true, producing an opt ref that has a value
// which is a reference pointing to null. Doing anything on that reference
// would cause a crash. The rest of this test is ensuring that case doesn't
// occur again.
if (context.has_value()) {
justin-mp marked this conversation as resolved.
Show resolved Hide resolved
// Compiler will not directly let us compare the rhs with null
// as it is assumed this is impossible.
void* foo = &context->get();
EXPECT_FALSE(foo == nullptr);
}
}

} // namespace
} // namespace Server
} // namespace Envoy