Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,20 @@ where

fn block_read(&mut self, id: BlockId, offset: u32, buf: &mut [u8]) -> Result<u32> {
let data = self.blocks.get(id).or_illegal_argument()?.data();
let data_len = data.len();

let len = if offset as usize >= data.len() {
0
} else {
buf.len().min(data.len())
};
// Calculate the number of bytes we are effectively copying.
// If the offset is beyond or equal to the length of data, we copy no bytes.
// Else, we copy as many bytes as the supplied buffer will allow.
let len = (offset as usize >= data_len)
.then_some(0)
.unwrap_or_else(|| buf.len().min(data_len - offset as usize)); // can't overflow

self.call_manager
.charge_gas(self.call_manager.price_list().on_block_read(len))?;

if len != 0 {
buf.copy_from_slice(&data[offset as usize..][..len]);
buf[..len].copy_from_slice(&data[offset as usize..][..len]);
}

Ok(len as u32)
Expand Down