From 2e00adf804f0ebacfe982063c0f98af836efd4f1 Mon Sep 17 00:00:00 2001 From: Matthew Gregan Date: Tue, 20 Jun 2017 15:26:59 +1200 Subject: Simplify Vec allocation in sendmsg after 077d979a removed alignment hacks. --- src/sys/socket/mod.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index a126f8b4..88fe02fe 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -258,14 +258,12 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<' len += cmsg.len(); capacity += cmsg.space(); } - // Alignment hackery. Note that capacity is guaranteed to be a - // multiple of size_t. Note also that the resulting vector claims - // to have length == capacity, so it's presently uninitialized. + // Note that the resulting vector claims to have length == capacity, + // so it's presently uninitialized. let mut cmsg_buffer = unsafe { let mut vec = Vec::::with_capacity(len); - let ptr = vec.as_mut_ptr(); - mem::forget(vec); - Vec::::from_raw_parts(ptr as *mut _, len, len) + vec.set_len(len); + vec }; { let mut ptr = &mut cmsg_buffer[..]; -- cgit v1.2.3 From 64815c61836ece60d6d01d1a1ecfc1877d5bb866 Mon Sep 17 00:00:00 2001 From: Matthew Gregan Date: Tue, 20 Jun 2017 15:30:48 +1200 Subject: Fix sendmsg on macOS when passing a zero entry cmsgs array. --- src/sys/socket/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 88fe02fe..c11b5367 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -277,12 +277,18 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<' None => (0 as *const _, 0), }; + let cmsg_ptr = if capacity > 0 { + cmsg_buffer.as_ptr() as *const c_void + } else { + ptr::null() + }; + let mhdr = msghdr { msg_name: name as *const c_void, msg_namelen: namelen, msg_iov: iov.as_ptr(), msg_iovlen: iov.len() as size_t, - msg_control: cmsg_buffer.as_ptr() as *const c_void, + msg_control: cmsg_ptr, msg_controllen: capacity as size_t, msg_flags: 0, }; -- cgit v1.2.3