Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions noq-udp/src/cmsg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ impl<'a, M: MsgHdr> Encoder<'a, M> {
///
/// # Panics
/// - If insufficient buffer space remains.
/// - If `T` has stricter alignment requirements than `M::ControlMessage`
pub(crate) fn push<T: Copy>(&mut self, level: c_int, ty: c_int, value: T) {
assert!(align_of::<T>() <= align_of::<M::ControlMessage>());
let space = M::ControlMessage::cmsg_space(size_of_val(&value));
assert!(
self.hdr.control_len() >= self.len + space,
Expand All @@ -54,7 +52,8 @@ impl<'a, M: MsgHdr> Encoder<'a, M> {
let cmsg = self.cmsg.take().expect("no control buffer space remaining");
cmsg.set(level, ty, M::ControlMessage::cmsg_len(size_of_val(&value)));
unsafe {
ptr::write(cmsg.cmsg_data() as *const T as *mut T, value);
// Unaligned: `align_of::<M::ControlMessage>()` is 4 on musl.
ptr::write_unaligned(cmsg.cmsg_data() as *const T as *mut T, value);
}
self.len += space;
self.cmsg = unsafe { self.hdr.cmsg_nxt_hdr(cmsg).as_mut() };
Expand All @@ -78,9 +77,10 @@ impl<M: MsgHdr> Drop for Encoder<'_, M> {
///
/// `cmsg` must refer to a native cmsg containing a payload of type `T`
pub(crate) unsafe fn decode<T: Copy, C: CMsgHdr>(cmsg: &impl CMsgHdr) -> T {
assert!(align_of::<T>() <= align_of::<C>());
debug_assert_eq!(cmsg.len(), C::cmsg_len(size_of::<T>()));
unsafe { ptr::read(cmsg.cmsg_data() as *const T) }
// Unaligned: `align_of::<libc::cmsghdr>()` is 4 on musl, but payloads such as
// `libc::timespec` (SCM_TIMESTAMPNS) need 8.
unsafe { ptr::read_unaligned(cmsg.cmsg_data() as *const T) }
}

pub(crate) struct Iter<'a, M: MsgHdr> {
Expand Down
Loading