From e0de7ef87b85891a31ffcaf90104b5db9393be18 Mon Sep 17 00:00:00 2001 From: yuslepukhin Date: Wed, 2 Dec 2015 14:50:33 -0800 Subject: [PATCH] Avoid empty ranges vector with subsequent zero element access --- db/db_table_properties_test.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/db/db_table_properties_test.cc b/db/db_table_properties_test.cc index 29afa0d9e..becf76e6f 100644 --- a/db/db_table_properties_test.cc +++ b/db/db_table_properties_test.cc @@ -92,6 +92,10 @@ TablePropertiesCollection DBTablePropertiesTest::TestGetPropertiesOfTablesInRange( std::vector ranges, std::size_t* num_properties, std::size_t* num_files) { + + // Since we deref zero element in the vector it can not be empty + // otherwise we pass an address to some random memory + EXPECT_GT(ranges.size(), 0U); // run the query TablePropertiesCollection props; EXPECT_OK(db_->GetPropertiesOfTablesInRange( @@ -101,10 +105,10 @@ DBTablePropertiesTest::TestGetPropertiesOfTablesInRange( // only which fall within requested ranges std::vector vmd; db_->GetLiveFilesMetaData(&vmd); - for (auto md : vmd) { + for (auto& md : vmd) { std::string fn = md.db_path + md.name; bool in_range = false; - for (auto r : ranges) { + for (auto& r : ranges) { // smallestkey < limit && largestkey >= start if (r.limit.compare(md.smallestkey) >= 0 && r.start.compare(md.largestkey) <= 0) { @@ -187,11 +191,20 @@ TEST_F(DBTablePropertiesTest, GetPropertiesOfTablesInRange) { for (int j = 0; j < 100; j++) { // create a bunch of ranges std::vector random_keys; - auto n = 2 * rnd.Uniform(50); + // Random returns numbers with zero included + // when we pass empty ranges TestGetPropertiesOfTablesInRange() + // derefs random memory in the empty ranges[0] + // so want to be greater than zero and even since + // the below loop requires that random_keys.size() to be even. + auto n = 2 * (rnd.Uniform(50) + 1); + for (uint32_t i = 0; i < n; ++i) { random_keys.push_back(test::RandomKey(&rnd, 5)); } + ASSERT_GT(random_keys.size(), 0U); + ASSERT_EQ((random_keys.size() % 2), 0U); + std::vector ranges; auto it = random_keys.begin(); while (it != random_keys.end()) {