From 5decdd74210c961d7cf4dd80a836863097dd5763 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 25 Apr 2020 15:39:03 -0600 Subject: [PATCH] Eliminate a mem::zeroed() in recvmmsg --- src/sys/socket/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 2d214e7ef8..9b770fa82b 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -982,16 +982,18 @@ pub fn recvmmsg<'a, I>( let mut output: Vec = Vec::with_capacity(num_messages); - // Addresses should be pre-allocated and never change the address during building - // of the input data for `recvmmsg` - let mut addresses: Vec = vec![unsafe { mem::zeroed() }; num_messages]; + // Addresses should be pre-allocated. pack_mhdr_to_receive will store them + // as raw pointers, so we may not move them. Turn the vec into a boxed + // slice so we won't inadvertently reallocate the vec. + let mut addresses = vec![mem::MaybeUninit::uninit(); num_messages] + .into_boxed_slice(); let results: Vec<_> = iter.enumerate().map(|(i, d)| { let (msg_controllen, mhdr) = unsafe { pack_mhdr_to_receive( d.iov.as_ref(), &mut d.cmsg_buffer, - &mut addresses[i], + addresses[i].as_mut_ptr(), ) }; @@ -1017,7 +1019,7 @@ pub fn recvmmsg<'a, I>( Ok(output .into_iter() - .zip(addresses.into_iter()) + .zip(addresses.iter().map(|addr| unsafe{addr.assume_init()})) .zip(results.into_iter()) .map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| { unsafe {