Skip to content

Commit dc331e2

Browse files
committed
Eliminate a mem::zeroed() in recvmmsg
1 parent cbeb0d2 commit dc331e2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/sys/socket/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,16 +941,18 @@ pub fn recvmmsg<'a, I>(
941941

942942
let mut output: Vec<libc::mmsghdr> = Vec::with_capacity(num_messages);
943943

944-
// Addresses should be pre-allocated and never change the address during building
945-
// of the input data for `recvmmsg`
946-
let mut addresses: Vec<sockaddr_storage> = vec![unsafe { mem::zeroed() }; num_messages];
944+
// Addresses should be pre-allocated. pack_mhdr_to_receive will store them
945+
// as raw pointers, so we may not move them. Turn the vec into a boxed
946+
// slice so we won't inadvertently reallocate the vec.
947+
let mut addresses = vec![mem::MaybeUninit::uninit(); num_messages]
948+
.into_boxed_slice();
947949

948950
let results: Vec<_> = iter.enumerate().map(|(i, d)| {
949951
let (msg_controllen, mhdr) = unsafe {
950952
pack_mhdr_to_receive(
951953
d.iov.as_ref(),
952954
&mut d.cmsg_buffer,
953-
&mut addresses[i],
955+
addresses[i].as_mut_ptr(),
954956
)
955957
};
956958

@@ -976,7 +978,7 @@ pub fn recvmmsg<'a, I>(
976978

977979
Ok(output
978980
.into_iter()
979-
.zip(addresses.into_iter())
981+
.zip(addresses.iter().map(|addr| unsafe{addr.assume_init()}))
980982
.zip(results.into_iter())
981983
.map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| {
982984
unsafe {

0 commit comments

Comments
 (0)