From c1a90b0848a290f921526c70ebea30bd7d8b9c58 Mon Sep 17 00:00:00 2001 From: Stanislau Hlebik Date: Mon, 21 Jul 2014 10:31:33 -0700 Subject: [PATCH] Fix db_bench Summary: Adding check for zero size index Test Plan: ./build_tools/regression_build_test.sh Reviewers: yhchiang, sdong Reviewed By: sdong Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D20259 --- table/plain_table_index.cc | 11 ++++++++--- table/plain_table_index.h | 2 +- table/plain_table_reader.cc | 9 ++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/table/plain_table_index.cc b/table/plain_table_index.cc index b3fc288ff..efba9b71d 100644 --- a/table/plain_table_index.cc +++ b/table/plain_table_index.cc @@ -16,15 +16,20 @@ inline uint32_t GetBucketIdFromHash(uint32_t hash, uint32_t num_buckets) { } } -void PlainTableIndex::InitFromRawData(Slice data) { - assert(GetVarint32(&data, &index_size_)); +Status PlainTableIndex::InitFromRawData(Slice data) { + if (!GetVarint32(&data, &index_size_)) { + return Status::Corruption("Couldn't read the index size!"); + } assert(index_size_ > 0); - assert(GetVarint32(&data, &num_prefixes_)); + if (!GetVarint32(&data, &num_prefixes_)) { + return Status::Corruption("Couldn't read the index size!"); + } sub_index_size_ = data.size() - index_size_ * kOffsetLen; char* index_data_begin = const_cast(data.data()); index_ = reinterpret_cast(index_data_begin); sub_index_ = reinterpret_cast(index_ + index_size_); + return Status::OK(); } PlainTableIndex::IndexSearchResult PlainTableIndex::GetOffset( diff --git a/table/plain_table_index.h b/table/plain_table_index.h index 347bb0f05..f63bbd0d5 100644 --- a/table/plain_table_index.h +++ b/table/plain_table_index.h @@ -72,7 +72,7 @@ class PlainTableIndex { IndexSearchResult GetOffset(uint32_t prefix_hash, uint32_t* bucket_value) const; - void InitFromRawData(Slice data); + Status InitFromRawData(Slice data); const char* GetSubIndexBasePtrAndUpperBound(uint32_t offset, uint32_t* upper_bound) const { diff --git a/table/plain_table_reader.cc b/table/plain_table_reader.cc index e20446f1e..8728eb1d3 100644 --- a/table/plain_table_reader.cc +++ b/table/plain_table_reader.cc @@ -236,8 +236,8 @@ Status PlainTableReader::PopulateIndexRecordList( } prefix_hashes->push_back(GetSliceHash(key_prefix_slice)); - index_.InitFromRawData(index_builder->Finish()); - return Status::OK(); + auto s = index_.InitFromRawData(index_builder->Finish()); + return s; } void PlainTableReader::AllocateAndFillBloom(int bloom_bits_per_key, @@ -357,7 +357,10 @@ Status PlainTableReader::PopulateIndex(TableProperties* props, return s; } } else { - index_.InitFromRawData(*index_block); + Status s = index_.InitFromRawData(*index_block); + if (!s.ok()) { + return s; + } } if (!index_in_file) {