From 905c23e90be1ab4c6bc91f18f80eba17bcbd0c10 Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Fri, 19 May 2023 16:40:23 -0700 Subject: Set the length of a sockaddr received on Linux --- src/sys/socket/addr.rs | 2 +- src/sys/socket/mod.rs | 34 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 48309749..8c5a56fe 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1679,7 +1679,7 @@ impl PartialEq for SockaddrStorage { } } -mod private { +pub(super) mod private { pub trait SockaddrLikePriv { /// Returns a mutable raw pointer to the inner structure. /// diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 9b6f18ef..1bbfa541 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1609,7 +1609,7 @@ impl MultiHeaders { { // we will be storing pointers to addresses inside mhdr - convert it into boxed // slice so it can'be changed later by pushing anything into self.addresses - let mut addresses = vec![std::mem::MaybeUninit::uninit(); num_slices].into_boxed_slice(); + let mut addresses = vec![std::mem::MaybeUninit::::uninit(); num_slices].into_boxed_slice(); let msg_controllen = cmsg_buffer.as_ref().map_or(0, |v| v.capacity()); @@ -1626,7 +1626,9 @@ impl MultiHeaders { Some(v) => ((&v[ix * msg_controllen] as *const u8), msg_controllen), None => (std::ptr::null(), 0), }; - let msg_hdr = unsafe { pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, address.as_mut_ptr()) }; + let msg_hdr = unsafe { + pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, ::as_mut_ptr(address.assume_init_mut()).cast()) + }; libc::mmsghdr { msg_hdr, msg_len: 0, @@ -1761,7 +1763,7 @@ where mmsghdr.msg_hdr, mmsghdr.msg_len as isize, self.rmm.msg_controllen, - address, + Some(address), ) }) } @@ -1914,7 +1916,7 @@ unsafe fn read_mhdr<'a, 'i, S>( mhdr: msghdr, r: isize, msg_controllen: usize, - address: S, + address: Option, ) -> RecvMsg<'a, 'i, S> where S: SockaddrLike { @@ -1933,7 +1935,7 @@ unsafe fn read_mhdr<'a, 'i, S>( RecvMsg { bytes: r as usize, cmsghdr, - address: Some(address), + address, flags: MsgFlags::from_bits_truncate(mhdr.msg_flags), mhdr, iobufs: std::marker::PhantomData, @@ -1951,22 +1953,19 @@ unsafe fn read_mhdr<'a, 'i, S>( /// headers are not used /// /// Buffers must remain valid for the whole lifetime of msghdr -unsafe fn pack_mhdr_to_receive( +unsafe fn pack_mhdr_to_receive( iov_buffer: *const IoSliceMut, iov_buffer_len: usize, cmsg_buffer: *const u8, cmsg_capacity: usize, - address: *mut S, -) -> msghdr - where - S: SockaddrLike -{ + address: *mut libc::sockaddr_storage, +) -> msghdr { // Musl's msghdr has private fields, so this is the only way to // initialize it. let mut mhdr = mem::MaybeUninit::::zeroed(); let p = mhdr.as_mut_ptr(); - (*p).msg_name = (*address).as_mut_ptr() as *mut c_void; - (*p).msg_namelen = S::size(); + (*p).msg_name = address as *mut c_void; + (*p).msg_namelen = mem::size_of::() as u32; (*p).msg_iov = iov_buffer as *mut iovec; (*p).msg_iovlen = iov_buffer_len as _; (*p).msg_control = cmsg_buffer as *mut c_void; @@ -2048,20 +2047,23 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i where S: SockaddrLike + 'a, 'inner: 'outer { - let mut address = mem::MaybeUninit::uninit(); + let mut address: libc::sockaddr_storage = unsafe { mem::MaybeUninit::zeroed().assume_init() }; + let address_ptr: *mut libc::sockaddr_storage = &mut address as *mut libc::sockaddr_storage; let (msg_control, msg_controllen) = cmsg_buffer.as_mut() .map(|v| (v.as_mut_ptr(), v.capacity())) .unwrap_or((ptr::null_mut(), 0)); let mut mhdr = unsafe { - pack_mhdr_to_receive(iov.as_ref().as_ptr(), iov.len(), msg_control, msg_controllen, address.as_mut_ptr()) + pack_mhdr_to_receive(iov.as_ref().as_ptr(), iov.len(), msg_control, msg_controllen, address_ptr) }; let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) }; let r = Errno::result(ret)?; - Ok(unsafe { read_mhdr(mhdr, r, msg_controllen, address.assume_init()) }) + let address = unsafe { S::from_raw(address_ptr.cast::(), Some(mhdr.msg_namelen)) }; + + Ok(unsafe { read_mhdr(mhdr, r, msg_controllen, address) }) } } -- cgit v1.2.3 From c80828e9dce12c5ae49fcb9510786a1d5e96aaa3 Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 22 May 2023 11:45:01 -0700 Subject: PR suggestion: Set the length later instead of using `from_raw` --- src/sys/socket/addr.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++++-- src/sys/socket/mod.rs | 30 ++++++++------- 2 files changed, 114 insertions(+), 17 deletions(-) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 8c5a56fe..12cdc7a4 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -763,6 +763,22 @@ impl SockaddrLike for UnixAddr { { mem::size_of::() as libc::socklen_t } + + unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + cfg_if! { + if #[cfg(any(target_os = "android", + target_os = "fuchsia", + target_os = "illumos", + target_os = "linux", + target_os = "redox", + ))] { + self.sun_len = new_length as u8; + } else { + self.sun.sun_len = new_length as u8; + } + }; + Ok(()) + } } impl AsRef for UnixAddr { @@ -912,8 +928,30 @@ pub trait SockaddrLike: private::SockaddrLikePriv { { mem::size_of::() as libc::socklen_t } + + /// Set the length of this socket address + /// + /// This method may only be called on socket addresses whose lenghts are dynamic, and it + /// returns an error if called on a type whose length is static. + /// + /// # Safety + /// + /// `new_length` must be a valid length for this type of address. Specifically, reads of that + /// length from `self` must be valid. + unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic>; } +/// The error returned by [`SockaddrLike::set_length`] on an address whose length is statically +/// fixed. +#[derive(Copy, Clone, Debug)] +pub struct SocketAddressLengthNotDynamic; +impl fmt::Display for SocketAddressLengthNotDynamic { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Attempted to set length on socket whose length is statically fixed") + } +} +impl std::error::Error for SocketAddressLengthNotDynamic {} + impl private::SockaddrLikePriv for () { fn as_mut_ptr(&mut self) -> *mut libc::sockaddr { ptr::null_mut() @@ -946,6 +984,10 @@ impl SockaddrLike for () { fn len(&self) -> libc::socklen_t { 0 } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } /// An IPv4 socket address @@ -1015,6 +1057,10 @@ impl SockaddrLike for SockaddrIn { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } #[cfg(feature = "net")] @@ -1134,6 +1180,10 @@ impl SockaddrLike for SockaddrIn6 { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } #[cfg(feature = "net")] @@ -1361,6 +1411,27 @@ impl SockaddrLike for SockaddrStorage { None => mem::size_of_val(self) as libc::socklen_t, } } + + unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + match self.as_unix_addr_mut() { + Some(addr) => { + cfg_if! { + if #[cfg(any(target_os = "android", + target_os = "fuchsia", + target_os = "illumos", + target_os = "linux", + target_os = "redox", + ))] { + addr.sun_len = new_length as u8; + } else { + addr.sun.sun_len = new_length as u8; + } + } + Ok(()) + }, + None => Err(SocketAddressLengthNotDynamic), + } + } } macro_rules! accessors { @@ -1754,6 +1825,10 @@ pub mod netlink { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for NetlinkAddr { @@ -1803,6 +1878,10 @@ pub mod alg { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for AlgAddr { @@ -1902,7 +1981,7 @@ pub mod sys_control { use std::{fmt, mem, ptr}; use std::os::unix::io::RawFd; use crate::{Errno, Result}; - use super::{private, SockaddrLike}; + use super::{private, SockaddrLike, SocketAddressLengthNotDynamic}; // FIXME: Move type into `libc` #[repr(C)] @@ -1943,6 +2022,10 @@ pub mod sys_control { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for SysControlAddr { @@ -2007,7 +2090,7 @@ pub mod sys_control { mod datalink { feature! { #![feature = "net"] - use super::{fmt, mem, private, ptr, SockaddrLike}; + use super::{fmt, mem, private, ptr, SockaddrLike, SocketAddressLengthNotDynamic}; /// Hardware Address #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -2085,6 +2168,10 @@ mod datalink { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for LinkAddr { @@ -2110,7 +2197,7 @@ mod datalink { mod datalink { feature! { #![feature = "net"] - use super::{fmt, mem, private, ptr, SockaddrLike}; + use super::{fmt, mem, private, ptr, SockaddrLike, SocketAddressLengthNotDynamic}; /// Hardware Address #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -2209,6 +2296,10 @@ mod datalink { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for LinkAddr { @@ -2257,6 +2348,10 @@ pub mod vsock { } Some(Self(ptr::read_unaligned(addr as *const _))) } + + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } impl AsRef for VsockAddr { diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 1bbfa541..78b7e8b0 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1627,7 +1627,7 @@ impl MultiHeaders { None => (std::ptr::null(), 0), }; let msg_hdr = unsafe { - pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, ::as_mut_ptr(address.assume_init_mut()).cast()) + pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, address.assume_init_mut()) }; libc::mmsghdr { msg_hdr, @@ -1763,7 +1763,7 @@ where mmsghdr.msg_hdr, mmsghdr.msg_len as isize, self.rmm.msg_controllen, - Some(address), + address, ) }) } @@ -1916,7 +1916,7 @@ unsafe fn read_mhdr<'a, 'i, S>( mhdr: msghdr, r: isize, msg_controllen: usize, - address: Option, + mut address: S, ) -> RecvMsg<'a, 'i, S> where S: SockaddrLike { @@ -1932,10 +1932,15 @@ unsafe fn read_mhdr<'a, 'i, S>( }.as_ref() }; + // Ignore errors if this socket address has statically-known length + // + // This is to ensure that unix socket addresses have their length set appropriately. + let _ = unsafe { address.set_length(mhdr.msg_namelen as usize) }; + RecvMsg { bytes: r as usize, cmsghdr, - address, + address: Some(address), flags: MsgFlags::from_bits_truncate(mhdr.msg_flags), mhdr, iobufs: std::marker::PhantomData, @@ -1953,19 +1958,19 @@ unsafe fn read_mhdr<'a, 'i, S>( /// headers are not used /// /// Buffers must remain valid for the whole lifetime of msghdr -unsafe fn pack_mhdr_to_receive( +unsafe fn pack_mhdr_to_receive( iov_buffer: *const IoSliceMut, iov_buffer_len: usize, cmsg_buffer: *const u8, cmsg_capacity: usize, - address: *mut libc::sockaddr_storage, -) -> msghdr { + address: *mut S, +) -> msghdr where S: SockaddrLike { // Musl's msghdr has private fields, so this is the only way to // initialize it. let mut mhdr = mem::MaybeUninit::::zeroed(); let p = mhdr.as_mut_ptr(); (*p).msg_name = address as *mut c_void; - (*p).msg_namelen = mem::size_of::() as u32; + (*p).msg_namelen = S::size(); (*p).msg_iov = iov_buffer as *mut iovec; (*p).msg_iovlen = iov_buffer_len as _; (*p).msg_control = cmsg_buffer as *mut c_void; @@ -2047,23 +2052,20 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i where S: SockaddrLike + 'a, 'inner: 'outer { - let mut address: libc::sockaddr_storage = unsafe { mem::MaybeUninit::zeroed().assume_init() }; - let address_ptr: *mut libc::sockaddr_storage = &mut address as *mut libc::sockaddr_storage; + let mut address: mem::MaybeUninit = mem::MaybeUninit::zeroed(); let (msg_control, msg_controllen) = cmsg_buffer.as_mut() .map(|v| (v.as_mut_ptr(), v.capacity())) .unwrap_or((ptr::null_mut(), 0)); let mut mhdr = unsafe { - pack_mhdr_to_receive(iov.as_ref().as_ptr(), iov.len(), msg_control, msg_controllen, address_ptr) + pack_mhdr_to_receive(iov.as_ref().as_ptr(), iov.len(), msg_control, msg_controllen, address.as_mut_ptr()) }; let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) }; let r = Errno::result(ret)?; - let address = unsafe { S::from_raw(address_ptr.cast::(), Some(mhdr.msg_namelen)) }; - - Ok(unsafe { read_mhdr(mhdr, r, msg_controllen, address) }) + Ok(unsafe { read_mhdr(mhdr, r, msg_controllen, address.assume_init()) }) } } -- cgit v1.2.3 From e5bd9ba193fe9e30108e0197d09aa8aa48c95d88 Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 22 May 2023 12:43:03 -0700 Subject: Fixup accidentally-introduced changes --- src/sys/socket/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/sys') diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 78b7e8b0..6690273b 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1626,9 +1626,7 @@ impl MultiHeaders { Some(v) => ((&v[ix * msg_controllen] as *const u8), msg_controllen), None => (std::ptr::null(), 0), }; - let msg_hdr = unsafe { - pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, address.assume_init_mut()) - }; + let msg_hdr = unsafe { pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, address.as_mut_ptr()) }; libc::mmsghdr { msg_hdr, msg_len: 0, @@ -1964,7 +1962,10 @@ unsafe fn pack_mhdr_to_receive( cmsg_buffer: *const u8, cmsg_capacity: usize, address: *mut S, -) -> msghdr where S: SockaddrLike { +) -> msghdr + where + S: SockaddrLike +{ // Musl's msghdr has private fields, so this is the only way to // initialize it. let mut mhdr = mem::MaybeUninit::::zeroed(); @@ -2052,7 +2053,7 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i where S: SockaddrLike + 'a, 'inner: 'outer { - let mut address: mem::MaybeUninit = mem::MaybeUninit::zeroed(); + let mut address = mem::MaybeUninit::zeroed(); let (msg_control, msg_controllen) = cmsg_buffer.as_mut() .map(|v| (v.as_mut_ptr(), v.capacity())) -- cgit v1.2.3 From ff0fb3d18776151d0c8694f564ffb21164d1e17f Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 22 May 2023 12:46:01 -0700 Subject: Remove redundant `unsafe` block --- src/sys/socket/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sys') diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 6690273b..ffab9741 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1933,7 +1933,7 @@ unsafe fn read_mhdr<'a, 'i, S>( // Ignore errors if this socket address has statically-known length // // This is to ensure that unix socket addresses have their length set appropriately. - let _ = unsafe { address.set_length(mhdr.msg_namelen as usize) }; + let _ = address.set_length(mhdr.msg_namelen as usize); RecvMsg { bytes: r as usize, -- cgit v1.2.3 From 012e788adcd8a41bdb028b5a6dc6dd1738dd2f9c Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 17 Jul 2023 11:33:43 -0700 Subject: More PR feedback --- src/sys/socket/addr.rs | 64 +++++++------------------------------------------- src/sys/socket/mod.rs | 2 +- 2 files changed, 10 insertions(+), 56 deletions(-) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 12cdc7a4..88bd6f5a 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -931,14 +931,17 @@ pub trait SockaddrLike: private::SockaddrLikePriv { /// Set the length of this socket address /// - /// This method may only be called on socket addresses whose lenghts are dynamic, and it + /// This method may only be called on socket addresses whose lengths are dynamic, and it /// returns an error if called on a type whose length is static. /// /// # Safety /// /// `new_length` must be a valid length for this type of address. Specifically, reads of that /// length from `self` must be valid. - unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic>; + #[doc(hidden)] + unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + Err(SocketAddressLengthNotDynamic) + } } /// The error returned by [`SockaddrLike::set_length`] on an address whose length is statically @@ -984,10 +987,6 @@ impl SockaddrLike for () { fn len(&self) -> libc::socklen_t { 0 } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } /// An IPv4 socket address @@ -1057,10 +1056,6 @@ impl SockaddrLike for SockaddrIn { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } #[cfg(feature = "net")] @@ -1180,10 +1175,6 @@ impl SockaddrLike for SockaddrIn6 { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } #[cfg(feature = "net")] @@ -1415,19 +1406,7 @@ impl SockaddrLike for SockaddrStorage { unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { match self.as_unix_addr_mut() { Some(addr) => { - cfg_if! { - if #[cfg(any(target_os = "android", - target_os = "fuchsia", - target_os = "illumos", - target_os = "linux", - target_os = "redox", - ))] { - addr.sun_len = new_length as u8; - } else { - addr.sun.sun_len = new_length as u8; - } - } - Ok(()) + addr.set_length(new_length) }, None => Err(SocketAddressLengthNotDynamic), } @@ -1825,10 +1804,6 @@ pub mod netlink { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for NetlinkAddr { @@ -1878,10 +1853,6 @@ pub mod alg { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for AlgAddr { @@ -1981,7 +1952,7 @@ pub mod sys_control { use std::{fmt, mem, ptr}; use std::os::unix::io::RawFd; use crate::{Errno, Result}; - use super::{private, SockaddrLike, SocketAddressLengthNotDynamic}; + use super::{private, SockaddrLike}; // FIXME: Move type into `libc` #[repr(C)] @@ -2022,10 +1993,6 @@ pub mod sys_control { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for SysControlAddr { @@ -2090,7 +2057,7 @@ pub mod sys_control { mod datalink { feature! { #![feature = "net"] - use super::{fmt, mem, private, ptr, SockaddrLike, SocketAddressLengthNotDynamic}; + use super::{fmt, mem, private, ptr, SockaddrLike}; /// Hardware Address #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -2168,10 +2135,6 @@ mod datalink { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for LinkAddr { @@ -2197,7 +2160,7 @@ mod datalink { mod datalink { feature! { #![feature = "net"] - use super::{fmt, mem, private, ptr, SockaddrLike, SocketAddressLengthNotDynamic}; + use super::{fmt, mem, private, ptr, SockaddrLike}; /// Hardware Address #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -2296,10 +2259,6 @@ mod datalink { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for LinkAddr { @@ -2307,7 +2266,6 @@ mod datalink { &self.0 } } - } } @@ -2348,10 +2306,6 @@ pub mod vsock { } Some(Self(ptr::read_unaligned(addr as *const _))) } - - unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { - Err(SocketAddressLengthNotDynamic) - } } impl AsRef for VsockAddr { diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index c77bc961..4ca12997 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -2053,7 +2053,7 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i where S: SockaddrLike + 'a, 'inner: 'outer { - let mut address = mem::MaybeUninit::zeroed(); + let mut address = mem::MaybeUninit::uninit(); let (msg_control, msg_controllen) = cmsg_buffer.as_mut() .map(|v| (v.as_mut_ptr(), v.capacity())) -- cgit v1.2.3 From 2378f12dd5a05751a79a0bf8553033c1fbece4f3 Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 17 Jul 2023 14:45:43 -0700 Subject: Correctly let the kernel set the length on BSDs Co-authored-by: Alan Somers --- src/sys/socket/addr.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 0517615e..5ec2fb5b 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -775,8 +775,6 @@ impl SockaddrLike for UnixAddr { target_os = "redox", ))] { self.sun_len = new_length as u8; - } else { - self.sun.sun_len = new_length as u8; } }; Ok(()) -- cgit v1.2.3 From 2d60044196bbcf6c4da187f413e7ddd00f0a2ae0 Mon Sep 17 00:00:00 2001 From: Jarred Allen Date: Mon, 17 Jul 2023 14:57:11 -0700 Subject: Fix lint --- src/sys/socket/addr.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 5ec2fb5b..1d86e756 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -767,6 +767,8 @@ impl SockaddrLike for UnixAddr { } unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> { + // `new_length` is only used on some platforms, so it must be provided even when not used + #![allow(unused_variables)] cfg_if! { if #[cfg(any(target_os = "android", target_os = "fuchsia", -- cgit v1.2.3