Skip to content

Commit aeac055

Browse files
committed
Hopefully unbreak sendmsg and recvmsg on OSX
64-bit OSX explicitly aligns cmsg_data to 32-bit boundaries.
1 parent 14fd402 commit aeac055

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/sys/socket/ffi.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ pub type type_of_cmsg_len = size_t;
1313
#[cfg(not(target_os = "linux"))]
1414
pub type type_of_cmsg_len = socklen_t;
1515

16+
// OSX always aligns struct cmsghdr as if it were a 32-bit OS
17+
#[cfg(target_os = "macos")]
18+
pub type type_of_cmsg_data = uint32_t;
19+
20+
#[cfg(not(target_os = "macos"))]
21+
pub type type_of_cmsg_data = size_t;
22+
1623
// Private because we don't expose any external functions that operate
1724
// directly on this type; we just use it internally at FFI boundaries.
1825
// Note that in some cases we store pointers in *const fields that the
@@ -37,7 +44,7 @@ pub struct cmsghdr {
3744
pub cmsg_len: type_of_cmsg_len,
3845
pub cmsg_level: c_int,
3946
pub cmsg_type: c_int,
40-
pub cmsg_data: [size_t; 0]
47+
pub cmsg_data: [type_of_cmsg_data; 0]
4148
}
4249

4350
extern {

src/sys/socket/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ unsafe fn copy_bytes<'a, 'b, T: ?Sized>(src: &T, dst: &'a mut &'b mut [u8]) {
9494
}
9595

9696

97-
use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len};
97+
use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len, type_of_cmsg_data};
9898

9999
/// A structure used to make room in a cmsghdr passed to recvmsg. The
100100
/// size and alignment match that of a cmsghdr followed by a T, but the
@@ -200,7 +200,7 @@ pub enum ControlMessage<'a> {
200200
pub struct UnknownCmsg<'a>(&'a cmsghdr, &'a [u8]);
201201

202202
fn cmsg_align(len: usize) -> usize {
203-
let align_bytes = mem::size_of::<size_t>() - 1;
203+
let align_bytes = mem::size_of::<type_of_cmsg_data>() - 1;
204204
(len + align_bytes) & !align_bytes
205205
}
206206

0 commit comments

Comments
 (0)