Skip to content

Commit

Permalink
Ensure read offset + data size doesn't exceed the max offset
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihuij committed Nov 16, 2023
1 parent 4b8e97f commit 1c525fc
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/mmap_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c525fc

Please sign in to comment.