
Dereferencing source_ (which is a std::optional<StatusOr<...>>) when the underlying StatusOr is not OK results in undefined behavior or an assertion failure. Since Metadata() returns a std::optional, we should safely return std::nullopt if source_ is not OK.
std::optional<google::bigtable::v2::ResultSetMetadata> Metadata() override {
if (!source_.has_value() || !source_->ok()) return std::nullopt;
return (**source_)->Metadata();
}
References
- Scrutinize Edge Cases: Always consider failure modes. Do not assume a perfect world. Also, never ignore Status types. (link)
- Prefer defensive code, such as explicit
ok() checks, even if they seem redundant based on the current implementation of a framework, as the framework's contract may change in the future.
Originally posted by @gemini-code-assist[bot] in #16215 (comment)
Dereferencing
source_(which is astd::optional<StatusOr<...>>) when the underlyingStatusOris not OK results in undefined behavior or an assertion failure. SinceMetadata()returns astd::optional, we should safely returnstd::nulloptifsource_is not OK.References
ok()checks, even if they seem redundant based on the current implementation of a framework, as the framework's contract may change in the future.Originally posted by @gemini-code-assist[bot] in #16215 (comment)