Skip to content

Commit

Permalink
Remove potential panic in split_init (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored and sunfishcode committed Oct 20, 2024
1 parent e19ec13 commit de3ac20
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ use core::slice;
///
/// # Safety
///
/// At least `init` bytes must be initialized.
/// At least `init_len` bytes must be initialized.
#[inline]
pub(super) unsafe fn split_init(
buf: &mut [MaybeUninit<u8>],
init: usize,
init_len: usize,
) -> (&mut [u8], &mut [MaybeUninit<u8>]) {
let (init, uninit) = buf.split_at_mut(init);
let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::<u8>(), init.len());
debug_assert!(init_len <= buf.len());
let buf_ptr = buf.as_mut_ptr();
let uninit_len = buf.len() - init_len;
let init = slice::from_raw_parts_mut(buf_ptr.cast::<u8>(), init_len);
let uninit = slice::from_raw_parts_mut(buf_ptr.add(init_len), uninit_len);
(init, uninit)
}

0 comments on commit de3ac20

Please sign in to comment.