Skip to content

Commit

Permalink
Fix Linux's CMSG_NXTHDR and CMSG_SPACE definitions.
Browse files Browse the repository at this point in the history
This is an error from PR rust-lang#1098.  The wrong definitions coincidentally
work on Linux/glibc, but fail on Linux/musl.
  • Loading branch information
asomers committed Feb 1, 2019
1 parent fc90925 commit 4557e50
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/unix/notbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,10 @@ pub const ARPHRD_IEEE802154: u16 = 804;
pub const ARPHRD_VOID: u16 = 0xFFFF;
pub const ARPHRD_NONE: u16 = 0xFFFE;

fn CMSG_ALIGN(len: usize) -> usize {
len + mem::size_of::<usize>() - 1 & !(mem::size_of::<usize>() - 1)
}

f! {
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
Expand All @@ -996,17 +1000,19 @@ f! {

pub fn CMSG_NXTHDR(mhdr: *const msghdr,
cmsg: *const cmsghdr) -> *mut cmsghdr {
if cmsg.is_null() {
return CMSG_FIRSTHDR(mhdr);
if (*cmsg).cmsg_len < mem::size_of::<cmsghdr>() {
return 0 as *mut cmsghdr;
};
let pad = mem::align_of::<cmsghdr>() - 1;
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize))
as *mut cmsghdr;
let max = (*mhdr).msg_control as usize
+ (*mhdr).msg_controllen as usize;
if next < max {
next as *mut cmsghdr
} else {
if (next.offset(1)) as usize > max
|| next as usize + CMSG_ALIGN((*next).cmsg_len) > max
{
0 as *mut cmsghdr
} else {
next as *mut cmsghdr
}
}

Expand All @@ -1015,12 +1021,12 @@ f! {
}

pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
(CMSG_ALIGN(length as usize) + CMSG_ALIGN(mem::size_of::<cmsghdr>()))
as ::c_uint
}

pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
mem::size_of::<cmsghdr>() as ::c_uint + length
CMSG_ALIGN(mem::size_of::<cmsghdr>()) as ::c_uint + length
}

pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
Expand Down

0 comments on commit 4557e50

Please sign in to comment.