Skip to content

Commit dbb87bb

Browse files
authored
[BugFix] Check read size before reading data block from datacache. (backport #38722) (#38799)
Signed-off-by: Gavin <[email protected]>
1 parent c07f462 commit dbb87bb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

be/src/io/cache_input_stream.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ Status CacheInputStream::read_at_fully(int64_t offset, void* out, int64_t count)
5454
char* pe = p + count;
5555

5656
auto read_one_block = [&](size_t offset, size_t size) {
57-
StatusOr<size_t> res;
57+
if (UNLIKELY(size == 0)) {
58+
return Status::OK();
59+
}
5860

61+
StatusOr<size_t> res;
5962
DCHECK(size <= BLOCK_SIZE);
6063
{
6164
SCOPED_RAW_TIMER(&_stats.read_cache_ns);

be/test/io/cache_input_stream_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,27 @@ TEST_F(CacheInputStreamTest, test_file_overwrite) {
189189
ASSERT_EQ(stats2.read_cache_count, 0);
190190
}
191191

192+
TEST_F(CacheInputStreamTest, test_read_with_zero_range) {
193+
const int64_t block_count = 1;
194+
int64_t data_size = block_size * block_count;
195+
char data[data_size + 1];
196+
gen_test_data(data, data_size, block_size);
197+
198+
std::shared_ptr<io::SeekableInputStream> stream(new MockSeekableInputStream(data, data_size));
199+
io::CacheInputStream cache_stream(stream, "test_file4", data_size, 1000000);
200+
cache_stream.set_enable_populate_cache(true);
201+
auto& stats = cache_stream.stats();
202+
203+
// read from backend, cache the data
204+
char buffer[block_size];
205+
read_stream_data(&cache_stream, 0, block_size, buffer);
206+
ASSERT_TRUE(check_data_content(buffer, block_size, 'a'));
207+
ASSERT_EQ(stats.read_cache_count, 0);
208+
ASSERT_EQ(stats.write_cache_count, 1);
209+
210+
// try read zero length data, expect no crash
211+
read_stream_data(&cache_stream, 0, 0, nullptr);
212+
ASSERT_EQ(stats.read_cache_count, 0);
213+
}
214+
192215
} // namespace starrocks::io

0 commit comments

Comments
 (0)