From 3ad6b794cf7b547e489489bc4af0e2b523b424be Mon Sep 17 00:00:00 2001 From: Igor Sugak Date: Tue, 17 Feb 2015 17:54:02 -0800 Subject: [PATCH] rocksdb: Fix 'Division by zero' scan-build warning Summary: scan-build complains with division by zero warning in a test. Added an assertion to prevent this. scan-build report: http://home.fburl.com/~sugak/latest6/report-c61be9.html#EndPath Test Plan: Make sure scan-build does not report 'Division by zero' and all tests are passing. ```lang=bash % make analyze % make check ``` Reviewers: igor, meyering Reviewed By: meyering Subscribers: sdong, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D33495 --- util/dynamic_bloom_test.cc | 2 +- util/env_posix.cc | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/util/dynamic_bloom_test.cc b/util/dynamic_bloom_test.cc index a8b1c529b..5e143551e 100644 --- a/util/dynamic_bloom_test.cc +++ b/util/dynamic_bloom_test.cc @@ -177,10 +177,10 @@ TEST(DynamicBloomTest, perf) { ++count; } } + ASSERT_EQ(count, num_keys); elapsed = timer.ElapsedNanos(); fprintf(stderr, "standard bloom, avg query latency %" PRIu64 "\n", elapsed / count); - ASSERT_TRUE(count == num_keys); // Locality enabled version DynamicBloom blocked_bloom(&arena, num_keys * 10, 1, num_probes); diff --git a/util/env_posix.cc b/util/env_posix.cc index 4adf58bcc..4d1e8e61d 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -401,16 +401,16 @@ class PosixMmapFile : public WritableFile { return s; } - bool UnmapCurrentRegion() { - bool result = true; + Status UnmapCurrentRegion() { TEST_KILL_RANDOM(rocksdb_kill_odds); if (base_ != nullptr) { if (last_sync_ < limit_) { // Defer syncing this data until next Sync() call, if any pending_sync_ = true; } - if (munmap(base_, limit_ - base_) != 0) { - result = false; + int munmap_status = munmap(base_, limit_ - base_); + if (munmap_status != 0) { + return IOError(filename_, munmap_status); } file_offset_ += limit_ - base_; base_ = nullptr; @@ -423,7 +423,7 @@ class PosixMmapFile : public WritableFile { map_size_ *= 2; } } - return result; + return Status::OK(); } Status MapNewRegion() { @@ -498,13 +498,15 @@ class PosixMmapFile : public WritableFile { assert(dst_ <= limit_); size_t avail = limit_ - dst_; if (avail == 0) { - if (UnmapCurrentRegion()) { - Status s = MapNewRegion(); - if (!s.ok()) { - return s; - } - TEST_KILL_RANDOM(rocksdb_kill_odds); + Status s = UnmapCurrentRegion(); + if (!s.ok()) { + return s; } + s = MapNewRegion(); + if (!s.ok()) { + return s; + } + TEST_KILL_RANDOM(rocksdb_kill_odds); } size_t n = (left <= avail) ? left : avail; @@ -524,7 +526,8 @@ class PosixMmapFile : public WritableFile { TEST_KILL_RANDOM(rocksdb_kill_odds); - if (!UnmapCurrentRegion()) { + s = UnmapCurrentRegion(); + if (!s.ok()) { s = IOError(filename_, errno); } else if (unused > 0) { // Trim the extra space at the end of the file