libsnapshot: Validate Sequence Op data

If the sequence op contains a Cow Operation that doesn't exist, we
should log the error and abort. Otherwise we'll try to merge an op that
doesn't exist.

Test: cow_api_test CowTest.MissingSeqOp
Bug: 177104308
Change-Id: I1a96f09798bc16126ae1338c6259506b18c88f51
This commit is contained in:
Daniel Rosenberg 2021-07-16 02:43:30 -07:00
parent 0b23b2a346
commit ab3b441f94
2 changed files with 28 additions and 0 deletions

View file

@ -1013,6 +1013,27 @@ TEST_F(CowTest, BigSeqOp) {
ASSERT_TRUE(iter->Done());
}
TEST_F(CowTest, MissingSeqOp) {
CowOptions options;
CowWriter writer(options);
const int seq_len = 10;
uint32_t sequence[seq_len];
for (int i = 0; i < seq_len; i++) {
sequence[i] = i + 1;
}
ASSERT_TRUE(writer.Initialize(cow_->fd));
ASSERT_TRUE(writer.AddSequenceData(seq_len, sequence));
ASSERT_TRUE(writer.AddZeroBlocks(1, seq_len - 1));
ASSERT_TRUE(writer.Finalize());
ASSERT_EQ(lseek(cow_->fd, 0, SEEK_SET), 0);
CowReader reader;
ASSERT_FALSE(reader.Parse(cow_->fd));
}
TEST_F(CowTest, RevMergeOpItrTest) {
CowOptions options;
options.cluster_ops = 5;

View file

@ -413,6 +413,13 @@ bool CowReader::PrepMergeOps() {
}
block_map->insert({current_op.new_block, i});
}
for (auto block : *merge_op_blocks) {
if (block_map->count(block) == 0) {
LOG(ERROR) << "Invalid Sequence Ops. Could not find Cow Op for new block " << block;
return false;
}
}
if (merge_op_blocks->size() > header_.num_merge_ops) {
num_ordered_ops_to_merge_ = merge_op_blocks->size() - header_.num_merge_ops;
} else {