Skip to content

Commit 2e14d95

Browse files
committed
Fix bugs found in CI:
* Specialize CMSG_NXTHDR for Android and Emscripten * Fix a typo in OSX * Restrict the cmsg tests to Unix platforms.
1 parent bdb622a commit 2e14d95

File tree

7 files changed

+62
-22
lines changed

7 files changed

+62
-22
lines changed

libc-test/build.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ extern crate ctest;
66
use std::env;
77

88
fn do_cc() {
9-
cc::Build::new()
10-
.file("src/cmsg.c")
11-
.compile("cmsg");
9+
#[cfg(unix)] {
10+
cc::Build::new()
11+
.file("src/cmsg.c")
12+
.compile("cmsg");
13+
}
1214
}
1315

1416
fn do_ctest() {

libc-test/test/cmsg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate libc;
55
use libc::{c_uchar, c_uint, c_void, cmsghdr, msghdr};
66
use std::{mem, ptr};
77

8+
#[cfg(unix)]
89
extern {
910
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
1011
pub fn cmsg_nxthdr(mhdr: *const msghdr,
@@ -14,6 +15,7 @@ extern {
1415
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar;
1516
}
1617

18+
#[cfg(unix)]
1719
#[test]
1820
fn test_cmsg_data() {
1921
for l in 0..128 {
@@ -24,6 +26,7 @@ fn test_cmsg_data() {
2426
}
2527
}
2628

29+
#[cfg(unix)]
2730
#[test]
2831
fn test_cmsg_firsthdr() {
2932
let mut mhdr: msghdr = unsafe{mem::zeroed()};
@@ -37,6 +40,7 @@ fn test_cmsg_firsthdr() {
3740
}
3841
}
3942

43+
#[cfg(unix)]
4044
#[test]
4145
fn test_cmsg_len() {
4246
for l in 0..128 {
@@ -46,6 +50,7 @@ fn test_cmsg_len() {
4650
}
4751
}
4852

53+
#[cfg(unix)]
4954
#[test]
5055
fn test_cmsg_nxthdr() {
5156
let mut buffer = [0u8; 256];
@@ -78,6 +83,7 @@ fn test_cmsg_nxthdr() {
7883
}
7984
}
8085

86+
#[cfg(unix)]
8187
#[test]
8288
fn test_cmsg_space() {
8389
unsafe {

src/unix/bsd/apple/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ f! {
24182418
}
24192419

24202420
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
2421-
__DARWIN_ALIGN32(mem::size_of::<::cmsghdr>() + length as usize)
2421+
__DARWIN_ALIGN32(mem::size_of::<::cmsghdr>()) + length as usize
24222422
as ::c_uint
24232423
}
24242424

src/unix/notbsd/android/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,20 @@ pub const MODULE_INIT_IGNORE_VERMAGIC: ::c_uint = 0x0002;
14771477
pub const ENOATTR: ::c_int = ::ENODATA;
14781478

14791479
f! {
1480+
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
1481+
cmsg: *const cmsghdr) -> *mut cmsghdr {
1482+
let next = (cmsg as usize
1483+
+ super::CMSG_ALIGN((*cmsg).cmsg_len as usize))
1484+
as *mut cmsghdr;
1485+
let max = (*mhdr).msg_control as usize
1486+
+ (*mhdr).msg_controllen as usize;
1487+
if (next.offset(1)) as usize > max {
1488+
0 as *mut cmsghdr
1489+
} else {
1490+
next as *mut cmsghdr
1491+
}
1492+
}
1493+
14801494
pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
14811495
for slot in cpuset.__bits.iter_mut() {
14821496
*slot = 0;

src/unix/notbsd/emscripten.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,23 @@ pub const ARPD_FLUSH: ::c_ushort = 0x03;
15101510
pub const ATF_MAGIC: ::c_int = 0x80;
15111511

15121512
f! {
1513+
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
1514+
cmsg: *const cmsghdr) -> *mut cmsghdr {
1515+
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1516+
return 0 as *mut cmsghdr;
1517+
};
1518+
let next = (cmsg as usize +
1519+
super::CMSG_ALIGN((*cmsg).cmsg_len as usize))
1520+
as *mut cmsghdr;
1521+
let max = (*mhdr).msg_control as usize
1522+
+ (*mhdr).msg_controllen as usize;
1523+
if (next.offset(1)) as usize > max {
1524+
0 as *mut cmsghdr
1525+
} else {
1526+
next as *mut cmsghdr
1527+
}
1528+
}
1529+
15131530
pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
15141531
for slot in cpuset.bits.iter_mut() {
15151532
*slot = 0;

src/unix/notbsd/linux/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,25 @@ pub const SOF_TIMESTAMPING_SYS_HARDWARE: ::c_uint = 1 << 5;
16971697
pub const SOF_TIMESTAMPING_RAW_HARDWARE: ::c_uint = 1 << 6;
16981698

16991699
f! {
1700+
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
1701+
cmsg: *const cmsghdr) -> *mut cmsghdr {
1702+
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1703+
return 0 as *mut cmsghdr;
1704+
};
1705+
let next = (cmsg as usize +
1706+
super::CMSG_ALIGN((*cmsg).cmsg_len as usize))
1707+
as *mut cmsghdr;
1708+
let max = (*mhdr).msg_control as usize
1709+
+ (*mhdr).msg_controllen as usize;
1710+
if (next.offset(1)) as usize > max ||
1711+
next as usize + super::CMSG_ALIGN((*next).cmsg_len as usize) > max
1712+
{
1713+
0 as *mut cmsghdr
1714+
} else {
1715+
next as *mut cmsghdr
1716+
}
1717+
}
1718+
17001719
pub fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
17011720
for slot in cpuset.bits.iter_mut() {
17021721
*slot = 0;

src/unix/notbsd/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -998,24 +998,6 @@ f! {
998998
}
999999
}
10001000

1001-
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
1002-
cmsg: *const cmsghdr) -> *mut cmsghdr {
1003-
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1004-
return 0 as *mut cmsghdr;
1005-
};
1006-
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize))
1007-
as *mut cmsghdr;
1008-
let max = (*mhdr).msg_control as usize
1009-
+ (*mhdr).msg_controllen as usize;
1010-
if (next.offset(1)) as usize > max
1011-
|| next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
1012-
{
1013-
0 as *mut cmsghdr
1014-
} else {
1015-
next as *mut cmsghdr
1016-
}
1017-
}
1018-
10191001
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut ::c_uchar {
10201002
cmsg.offset(1) as *mut ::c_uchar
10211003
}

0 commit comments

Comments
 (0)