From fcce1f2c7abc89470e7641978b210a0eeaeec7b8 Mon Sep 17 00:00:00 2001 From: sdong Date: Tue, 21 Sep 2021 12:21:03 -0700 Subject: [PATCH] RandomAccessFileReader::MultiRead() should not return read bytes not read (#8941) Summary: Right now, if underlying read returns fewer bytes than asked for, RandomAccessFileReader::MultiRead() still returns those in the buffer to upper layer. This can be a surprise to upper layer. This is unlikely to cause incorrect data. To cause incorrect data, checksum checking in upper layer should pass with short reads, whose chance is low. Pull Request resolved: https://github.com/facebook/rocksdb/pull/8941 Test Plan: Run stress tests for a while Reviewed By: anand1976 Differential Revision: D31085780 fbshipit-source-id: 999adf2d6c2712f1323d14bb68b678df59969973 --- file/random_access_file_reader.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/file/random_access_file_reader.cc b/file/random_access_file_reader.cc index e15b6b033..9bad958c4 100644 --- a/file/random_access_file_reader.cc +++ b/file/random_access_file_reader.cc @@ -296,8 +296,14 @@ IOStatus RandomAccessFileReader::MultiRead(const IOOptions& opts, r.status = fs_r.status; if (r.status.ok()) { uint64_t offset = r.offset - fs_r.offset; - size_t len = std::min(r.len, static_cast(fs_r.len - offset)); - r.result = Slice(fs_r.scratch + offset, len); + if (fs_r.result.size() <= offset) { + // No byte in the read range is returned. + r.result = Slice(); + } else { + size_t len = std::min( + r.len, static_cast(fs_r.result.size() - offset)); + r.result = Slice(fs_r.scratch + offset, len); + } } else { r.result = Slice(); }