Skip to content

Bigtable DataConnectionImpl::Metadata could dereference non-OK StatusOr #16222

Description

@scotthart

high

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
  1. Scrutinize Edge Cases: Always consider failure modes. Do not assume a perfect world. Also, never ignore Status types. (link)
  2. 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)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions