Skip to content

Commit

Permalink
Refactors unsafe code in AppendVec::get_slice() (#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Jul 16, 2024
1 parent adf2447 commit be0f784
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions accounts-db/src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,13 @@ impl AppendVec {
/// Also return the offset of the first byte after the requested data that
/// falls on a 64-byte boundary.
fn get_slice(slice: ValidSlice, offset: usize, size: usize) -> Option<(&[u8], usize)> {
let (next, overflow) = offset.overflowing_add(size);
if overflow || next > slice.0.len() {
return None;
}
let data = &slice.0[offset..next];
let next = u64_align!(next);

Some((
//UNSAFE: This unsafe creates a slice that represents a chunk of self.map memory
//The lifetime of this slice is tied to &self, since it points to self.map memory
unsafe { std::slice::from_raw_parts(data.as_ptr(), size) },
next,
))
// SAFETY: Wrapping math is safe here because if `end` does wrap, the Range
// parameter to `.get()` will be invalid, and `.get()` will correctly return None.
let end = offset.wrapping_add(size);
slice
.0
.get(offset..end)
.map(|subslice| (subslice, u64_align!(end)))
}

/// Copy `len` bytes from `src` to the first 64-byte boundary after position `offset` of
Expand Down

0 comments on commit be0f784

Please sign in to comment.