Skip to content

Commit 50e77a8

Browse files
pkastingcmumford
authored andcommitted
Fix size_t/int comparison/conversion issues in leveldb.
The create function took |num_keys| as an int, but callers and implementers wanted it to function as a size_t (e.g. passing std::vector::size() in, passing it to vector constructors as a size arg, indexing containers by it, etc.). This resulted in implicit conversions between the two types as well as warnings (found with Chromium's external copy of these sources, built with MSVC) about signed vs. unsigned comparisons. The leveldb sources were already widely using size_t elsewhere, e.g. for key and filter lengths, so using size_t here is not inconsistent with the existing code. However, it does change the public C API. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=101074871
1 parent 5208e79 commit 50e77a8

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

table/filter_block.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void FilterBlockBuilder::GenerateFilter() {
6868

6969
// Generate filter for current set of keys and append to result_.
7070
filter_offsets_.push_back(result_.size());
71-
policy_->CreateFilter(&tmp_keys_[0], num_keys, &result_);
71+
policy_->CreateFilter(&tmp_keys_[0], static_cast<int>(num_keys), &result_);
7272

7373
tmp_keys_.clear();
7474
keys_.clear();
@@ -97,7 +97,7 @@ bool FilterBlockReader::KeyMayMatch(uint64_t block_offset, const Slice& key) {
9797
if (index < num_) {
9898
uint32_t start = DecodeFixed32(offset_ + index*4);
9999
uint32_t limit = DecodeFixed32(offset_ + index*4 + 4);
100-
if (start <= limit && limit <= (offset_ - data_)) {
100+
if (start <= limit && limit <= static_cast<size_t>(offset_ - data_)) {
101101
Slice filter = Slice(data_ + start, limit - start);
102102
return policy_->KeyMayMatch(key, filter);
103103
} else if (start == limit) {

util/bloom.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BloomFilterPolicy : public FilterPolicy {
4747
dst->resize(init_size + bytes, 0);
4848
dst->push_back(static_cast<char>(k_)); // Remember # of probes in filter
4949
char* array = &(*dst)[init_size];
50-
for (size_t i = 0; i < n; i++) {
50+
for (int i = 0; i < n; i++) {
5151
// Use double-hashing to generate a sequence of hash values.
5252
// See analysis in [Kirsch,Mitzenmacher 2006].
5353
uint32_t h = BloomHash(keys[i]);

util/bloom_test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class BloomTest {
4646
key_slices.push_back(Slice(keys_[i]));
4747
}
4848
filter_.clear();
49-
policy_->CreateFilter(&key_slices[0], key_slices.size(), &filter_);
49+
policy_->CreateFilter(&key_slices[0], static_cast<int>(key_slices.size()),
50+
&filter_);
5051
keys_.clear();
5152
if (kVerbose >= 2) DumpFilter();
5253
}

0 commit comments

Comments
 (0)