Skip to content

Commit 4a70c93

Browse files
committed
Fix Linux's CMSG_NXTHDR and CMSG_SPACE definitions.
This is an error from PR #1098. The wrong definitions coincidentally work on Linux/glibc, but fail on Linux/musl.
1 parent fc90925 commit 4a70c93

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/unix/notbsd/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,10 @@ pub const ARPHRD_IEEE802154: u16 = 804;
985985
pub const ARPHRD_VOID: u16 = 0xFFFF;
986986
pub const ARPHRD_NONE: u16 = 0xFFFE;
987987

988+
fn CMSG_ALIGN(len: usize) -> usize {
989+
len + mem::size_of::<usize>() - 1 & !(mem::size_of::<usize>() - 1)
990+
}
991+
988992
f! {
989993
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
990994
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
@@ -996,17 +1000,19 @@ f! {
9961000

9971001
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
9981002
cmsg: *const cmsghdr) -> *mut cmsghdr {
999-
if cmsg.is_null() {
1000-
return CMSG_FIRSTHDR(mhdr);
1003+
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1004+
return 0 as *mut cmsghdr;
10011005
};
1002-
let pad = mem::align_of::<cmsghdr>() - 1;
1003-
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
1006+
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize))
1007+
as *mut cmsghdr;
10041008
let max = (*mhdr).msg_control as usize
10051009
+ (*mhdr).msg_controllen as usize;
1006-
if next < max {
1007-
next as *mut cmsghdr
1008-
} else {
1010+
if (next.offset(1)) as usize > max
1011+
|| next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
1012+
{
10091013
0 as *mut cmsghdr
1014+
} else {
1015+
next as *mut cmsghdr
10101016
}
10111017
}
10121018

@@ -1015,12 +1021,12 @@ f! {
10151021
}
10161022

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

10221028
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
1023-
mem::size_of::<cmsghdr>() as ::c_uint + length
1029+
CMSG_ALIGN(mem::size_of::<cmsghdr>()) as ::c_uint + length
10241030
}
10251031

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

0 commit comments

Comments
 (0)