From f0be3b29a8c944c12049b8afdc76efb4f3aa844f Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Thu, 17 Oct 2024 20:43:44 +0300 Subject: [PATCH] Remove potential panic in `split_init` (#1184) --- src/buffer.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index d7c0924ce..1e705df3f 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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], - init: usize, + init_len: usize, ) -> (&mut [u8], &mut [MaybeUninit]) { - let (init, uninit) = buf.split_at_mut(init); - let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::(), 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::(), init_len); + let uninit = slice::from_raw_parts_mut(buf_ptr.add(init_len), uninit_len); (init, uninit) }