From 1c525fc3df132bd7f4c26ee5439bf4425bd90be3 Mon Sep 17 00:00:00 2001 From: Zhihui Jiao Date: Thu, 16 Nov 2023 23:36:04 +0800 Subject: [PATCH] Ensure read offset + data size doesn't exceed the max offset --- src/mmap_file.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/mmap_file.rs b/src/mmap_file.rs index 352e6dc..5573652 100644 --- a/src/mmap_file.rs +++ b/src/mmap_file.rs @@ -22,7 +22,6 @@ impl MemoryMappedFile { let mmap = unsafe { MmapMut::map_mut(&file).context(StdIOSnafu)? }; - // TODO max_offset? Ok(MemoryMappedFile { mmap, min_offset: start_offset, max_offset: start_offset }) } @@ -78,9 +77,8 @@ impl MemoryMappedFile { let mut buffer = vec![0; data_size]; let read_pos = offset - self.min_offset; - // TODO mmap.len should be max_offset? - // Ensure the buffer size matches the mapped region. - if read_pos + data_size < self.mmap.len() { + // Ensure read offset + data size doesn't exceed the max offset. + if read_pos + data_size <= self.max_offset { buffer.copy_from_slice(&self.mmap[read_pos..read_pos + data_size]); Ok(buffer)