From 6ec44bd5b6a354c55f3f5175f55d26534ee5b886 Mon Sep 17 00:00:00 2001 From: Joey Sacchini Date: Mon, 4 Jan 2021 21:19:52 -0500 Subject: remove some unsafe code and add max packet size (also add some debug assertions) --- src/util.rs | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index e33fc10..3f4bd9f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,18 +17,8 @@ pub(crate) fn get_sized_buf(buf: &mut Option>, offset: usize, size: usiz } fn ensure_buf_has_size(buf: &mut Vec, total_size: usize) { - let cur_len = buf.len(); - if cur_len >= total_size { - return; - } - - let additional = total_size - cur_len; - buf.reserve(additional); - unsafe { - let start_at = buf.as_mut_ptr(); - let start_write_at = start_at.offset(cur_len as isize); - std::ptr::write_bytes(start_write_at, 0, additional); - buf.set_len(total_size); + if total_size > buf.len() { + buf.resize(total_size, 0u8); } } @@ -42,15 +32,5 @@ pub(crate) fn move_data_rightwards(target: &mut [u8], size: usize, shift_amount: ) } - unsafe { move_data_rightwards_unchecked(target, size, shift_amount) } -} - -unsafe fn move_data_rightwards_unchecked(target: &mut [u8], size: usize, shift_amount: usize) { - if shift_amount == 0 { - return; - } - - let src_ptr = target.as_mut_ptr(); - let dst_ptr = src_ptr.offset(shift_amount as isize); - std::ptr::copy(src_ptr, dst_ptr, size); -} + target.copy_within(0..size, shift_amount); +} \ No newline at end of file -- cgit