diff --git a/util/memenv.cc b/util/memenv.cc index 5737370230..835de6e881 100644 --- a/util/memenv.cc +++ b/util/memenv.cc @@ -2,14 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. +#include + +#include +#include +#include +#include + #include "rocksdb/env.h" #include "rocksdb/status.h" #include "port/port.h" #include "util/mutexlock.h" -#include -#include -#include -#include namespace rocksdb { @@ -70,10 +73,7 @@ class FileState { uint64_t Size() const { return size_; } Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const { - if (offset > size_) { - return Status::IOError("Offset greater than file size."); - } - const uint64_t available = size_ - offset; + const uint64_t available = size_ - std::min(size_, offset); if (n > available) { n = available; } diff --git a/util/memenv_test.cc b/util/memenv_test.cc index 2b872d266c..0df5eb71c1 100644 --- a/util/memenv_test.cc +++ b/util/memenv_test.cc @@ -124,7 +124,7 @@ TEST_F(MemEnvTest, ReadWrite) { ASSERT_EQ(0, result.compare("d")); // Too high offset. - ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok()); + ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok()); } TEST_F(MemEnvTest, Locks) { diff --git a/util/mock_env.cc b/util/mock_env.cc index 5001dfc395..cbc0e282b4 100644 --- a/util/mock_env.cc +++ b/util/mock_env.cc @@ -99,10 +99,7 @@ class MemFile { Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const { MutexLock lock(&mutex_); - if (offset > Size()) { - return Status::IOError("Offset greater than file size."); - } - const uint64_t available = Size() - offset; + const uint64_t available = Size() - std::min(Size(), offset); if (n > available) { n = available; } diff --git a/util/mock_env_test.cc b/util/mock_env_test.cc index 710881b553..5437060510 100644 --- a/util/mock_env_test.cc +++ b/util/mock_env_test.cc @@ -123,7 +123,7 @@ TEST_F(MockEnvTest, ReadWrite) { ASSERT_EQ(0, result.compare("d")); // Too high offset. - ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok()); + ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok()); } TEST_F(MockEnvTest, Locks) {