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

fix(spanner): avoid use-after-move bugs #7588

Merged
merged 2 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions google/cloud/spanner/row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ bool operator==(Row const& a, Row const& b) {
RowStreamIterator::RowStreamIterator() = default;

RowStreamIterator::RowStreamIterator(Source source)
: row_(Row{}), source_(std::move(source)) {
: source_(std::move(source)) {
++*this;
}

RowStreamIterator& RowStreamIterator::operator++() {
if (!row_) {
if (!row_ok_) {
source_ = nullptr; // Last row was an error; become "end"
return *this;
}
row_ = source_();
row_ok_ = row_.ok();
if (row_ && row_->size() == 0) {
source_ = nullptr; // No more Rows to consume; become "end"
return *this;
Expand Down
7 changes: 5 additions & 2 deletions google/cloud/spanner/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ class RowStreamIterator {
friend bool operator!=(RowStreamIterator const&, RowStreamIterator const&);

private:
value_type row_;
bool row_ok_{true};
value_type row_{Row{}};
Source source_; // nullptr means "end"
};

Expand Down Expand Up @@ -343,7 +344,7 @@ class TupleStreamIterator {
const_pointer operator->() const { return &tup_; }

TupleStreamIterator& operator++() {
if (!tup_) {
if (!tup_ok_) {
it_ = end_;
return *this;
}
Expand Down Expand Up @@ -372,8 +373,10 @@ class TupleStreamIterator {
void ParseTuple() {
if (it_ == end_) return;
tup_ = *it_ ? std::move(*it_)->template get<Tuple>() : it_->status();
tup_ok_ = tup_.ok();
}

bool tup_ok_{false};
value_type tup_;
RowStreamIterator it_;
RowStreamIterator end_;
Expand Down
54 changes: 54 additions & 0 deletions google/cloud/spanner/row_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,34 @@ TEST(RowStreamIterator, RangeForLoop) {
EXPECT_EQ(product, 30);
}

TEST(RowStreamIterator, MovedFromValueOk) {
std::vector<Row> rows;
rows.emplace_back(MakeTestRow({{"num", Value(1)}}));
rows.emplace_back(MakeTestRow({{"num", Value(2)}}));

RowRange range(MakeRowStreamIteratorSource(rows));
auto it = range.begin();
auto end = range.end();

EXPECT_NE(it, end);
auto row = std::move(*it);
EXPECT_STATUS_OK(row);
auto val = row->get("num");
EXPECT_STATUS_OK(val);
EXPECT_EQ(Value(1), *val);

++it;
EXPECT_NE(it, end);
row = std::move(*it);
EXPECT_STATUS_OK(row);
val = row->get("num");
EXPECT_STATUS_OK(val);
EXPECT_EQ(Value(2), *val);

++it;
EXPECT_EQ(it, end);
}

TEST(TupleStreamIterator, Basics) {
std::vector<Row> rows;
rows.emplace_back(MakeTestRow(1, "foo", true));
Expand Down Expand Up @@ -404,6 +432,32 @@ TEST(TupleStreamIterator, Error) {
EXPECT_EQ(it, end);
}

TEST(TupleStreamIterator, MovedFromValueOk) {
std::vector<Row> rows;
rows.emplace_back(MakeTestRow({{"num", Value(1)}}));
rows.emplace_back(MakeTestRow({{"num", Value(2)}}));

RowRange range(MakeRowStreamIteratorSource(rows));
using RowType = std::tuple<std::int64_t>;
using TupleIterator = TupleStreamIterator<RowType>;
auto it = TupleIterator(range.begin(), range.end());
auto end = TupleIterator();

EXPECT_NE(it, end);
auto tup = std::move(*it);
EXPECT_STATUS_OK(tup);
EXPECT_EQ(1, std::get<0>(*tup));

++it;
EXPECT_NE(it, end);
tup = std::move(*it);
EXPECT_STATUS_OK(tup);
EXPECT_EQ(2, std::get<0>(*tup));

++it;
EXPECT_EQ(it, end);
}

TEST(TupleStream, Basics) {
std::vector<Row> rows;
rows.emplace_back(MakeTestRow(1, "foo", true));
Expand Down