chore(bigtable): migrate to std::optional#16215
Conversation
There was a problem hiding this comment.
Code Review
This pull request migrates the codebase from absl::optional to C++17's std::optional (along with corresponding changes to nullopt and make_optional) across multiple BigQuery and Bigtable files. Feedback on the changes highlights a safety issue in google/cloud/bigtable/internal/data_connection_impl.cc, where source_ is dereferenced without checking if the underlying StatusOr is OK, potentially leading to undefined behavior or assertion failures.
| std::optional<google::bigtable::v2::ResultSetMetadata> Metadata() override { | ||
| if (!source_.has_value()) return std::nullopt; | ||
| return (**source_)->Metadata(); | ||
| } |
There was a problem hiding this comment.
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.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #16215 +/- ##
=======================================
Coverage 92.23% 92.24%
=======================================
Files 2265 2265
Lines 210205 210210 +5
=======================================
+ Hits 193888 193899 +11
+ Misses 16317 16311 -6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
248d4a2 to
cd20806
Compare
cd20806 to
06930ef
Compare
Replacing explicit usages of absl::optional with std::optional in the Bigtable Client library as part of broader modernization efforts