Skip to content

Commit

Permalink
fix: avoid use-after-move StatusOr
Browse files Browse the repository at this point in the history
Similar to: googleapis#7588

This code allowed callers to (rightfully) move-out the `StatusOr` that
was held in `current_`. Therefore, it's a bug if we then consult the
status of `current_.ok()`, because it may be in a moved-from state.

The fix is to track the `current_ok_` bit separately, so that we only
ever assign to `current_` and never read it after it could have been
moved.
  • Loading branch information
devjgm committed Nov 19, 2021
1 parent 72a9ed2 commit 15f5ba0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion google/cloud/stream_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class StreamRange {
private:
void Next() {
// Jump to the end if we previously got an error.
if (!is_end_ && !current_) {
if (!is_end_ && !current_ok_) {
is_end_ = true;
return;
}
Expand All @@ -178,10 +178,12 @@ class StreamRange {
void operator()(Status&& status) {
sr.is_end_ = status.ok();
if (!status.ok()) sr.current_ = std::move(status);
sr.current_ok_ = sr.current_.ok();
}
void operator()(T&& t) {
sr.is_end_ = false;
sr.current_ = std::move(t);
sr.current_ok_ = sr.current_.ok();
}
};
auto v = reader_();
Expand Down Expand Up @@ -224,6 +226,7 @@ class StreamRange {

internal::StreamReader<T> reader_;
StatusOr<T> current_;
bool current_ok_ = false;
bool is_end_ = true;
};

Expand Down

0 comments on commit 15f5ba0

Please sign in to comment.