Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ipld::read does not use offset properly #542

Closed
arajasek opened this issue May 9, 2022 · 0 comments · Fixed by #533
Closed

ipld::read does not use offset properly #542

arajasek opened this issue May 9, 2022 · 0 comments · Fixed by #533

Comments

@arajasek
Copy link
Contributor

arajasek commented May 9, 2022

Given a block id, ipld::read reads a block from the kernel’s BlockRegistry into a provided buffer. ipld::read also accepts an offset into the block where the read should begin.

offset is passed into Kernel::block_read, which first calculates the length of the read to perform before copying block data to the output buffer:

let len = if offset as usize >= data.len() {
  0
} else {
  buf.len().min(data.len())
};

// ...

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

Neither operation correctly handles nonzero offsets:

  • The read length should be buf.len().min(data.len() - offset)
  • The copy operation should be buf[..len].copy_from_slice(...)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
1 participant