Skip to content

Commit 4300666

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 73df81f commit 4300666

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
@@ -1114,6 +1114,10 @@ pub const ARPHRD_IEEE802154: u16 = 804;
11141114
pub const ARPHRD_VOID: u16 = 0xFFFF;
11151115
pub const ARPHRD_NONE: u16 = 0xFFFE;
11161116

1117+
fn CMSG_ALIGN(len: usize) -> usize {
1118+
len + mem::size_of::<usize>() - 1 & !(mem::size_of::<usize>() - 1)
1119+
}
1120+
11171121
f! {
11181122
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
11191123
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
@@ -1125,17 +1129,19 @@ f! {
11251129

11261130
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
11271131
cmsg: *const cmsghdr) -> *mut cmsghdr {
1128-
if cmsg.is_null() {
1129-
return CMSG_FIRSTHDR(mhdr);
1132+
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1133+
return 0 as *mut cmsghdr;
11301134
};
1131-
let pad = mem::align_of::<cmsghdr>() - 1;
1132-
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
1135+
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize))
1136+
as *mut cmsghdr;
11331137
let max = (*mhdr).msg_control as usize
11341138
+ (*mhdr).msg_controllen as usize;
1135-
if next < max {
1136-
next as *mut cmsghdr
1137-
} else {
1139+
if (next.offset(1)) as usize > max
1140+
|| next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
1141+
{
11381142
0 as *mut cmsghdr
1143+
} else {
1144+
next as *mut cmsghdr
11391145
}
11401146
}
11411147

@@ -1144,12 +1150,12 @@ f! {
11441150
}
11451151

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

11511157
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
1152-
mem::size_of::<cmsghdr>() as ::c_uint + length
1158+
CMSG_ALIGN(mem::size_of::<cmsghdr>()) as ::c_uint + length
11531159
}
11541160

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

0 commit comments

Comments
 (0)