Skip to content

Commit c88f3c8

Browse files
committed
Use size_t for parameter and return value of CMSG_{LEN,SPACE}
Fixes: #3240 Matches all platforms except Solaris and its descendants. `size_t` is used there regardless in order to force excessive conversions, and to make cross-platform code easier to write. [illumos]: https://github.com/illumos/illumos-gate/blob/118b2dbf1f4a745a7e35a5054a777c09bd90fff7/usr/src/uts/common/sys/socket.h#L499-L505
1 parent 3bd7dcb commit c88f3c8

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

libc-test/tests/cmsg.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ mod t {
77

88
use libc::{
99
c_uchar,
10-
c_uint,
1110
c_void,
1211
cmsghdr,
1312
msghdr,
13+
size_t,
1414
};
1515

1616
extern "C" {
1717
pub fn cmsg_firsthdr(msgh: *const msghdr) -> *mut cmsghdr;
1818
pub fn cmsg_nxthdr(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr;
19-
pub fn cmsg_space(length: c_uint) -> usize;
20-
pub fn cmsg_len(length: c_uint) -> usize;
19+
pub fn cmsg_space(length: size_t) -> size_t;
20+
pub fn cmsg_len(length: size_t) -> size_t;
2121
pub fn cmsg_data(cmsg: *const cmsghdr) -> *mut c_uchar;
2222
}
2323

@@ -47,8 +47,8 @@ mod t {
4747

4848
#[test]
4949
fn test_cmsg_len() {
50-
for l in 0..128 {
51-
assert_eq!(libc::CMSG_LEN(l) as usize, unsafe { cmsg_len(l) });
50+
for l in 0..128usize {
51+
assert_eq!(libc::CMSG_LEN(l), unsafe { cmsg_len(l) });
5252
}
5353
}
5454

@@ -75,8 +75,7 @@ mod t {
7575

7676
while !current_cmsghdr_ptr.is_null() {
7777
unsafe {
78-
(*current_cmsghdr_ptr).cmsg_len =
79-
libc::CMSG_LEN(cmsg_payload_len as _) as _;
78+
(*current_cmsghdr_ptr).cmsg_len = libc::CMSG_LEN(cmsg_payload_len) as _;
8079

8180
let libc_next = libc::CMSG_NXTHDR(&mhdr, current_cmsghdr_ptr);
8281
let system_next = cmsg_nxthdr(&mhdr, current_cmsghdr_ptr);
@@ -101,7 +100,7 @@ mod t {
101100
#[test]
102101
fn test_cmsg_space() {
103102
for l in 0..128 {
104-
assert_eq!(libc::CMSG_SPACE(l) as usize, unsafe { cmsg_space(l) });
103+
assert_eq!(libc::CMSG_SPACE(l), unsafe { cmsg_space(l) });
105104
}
106105
}
107106
}

src/new/common/posix/sys/socket.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,15 @@ pub(crate) const fn align_impl(len: usize, align: usize) -> usize {
2323
(len + mask) & !mask
2424
}
2525

26-
// TODO(#3240): consider changing signatures to `CMSG_{SPACE,LEN}(length: size_t) -> size_`
27-
2826
/// Total length of a non-padded control message for a payload of size `length`.
2927
///
3028
/// This function is almost exclusively used setting [`cmsghdr::cmsg_len`].
3129
/// It should *not* be used for determining the actual ancillary data buffer
3230
/// size. [`CMSG_SPACE`] should instead be for that.
3331
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
34-
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
35-
let length = length as size_t;
36-
32+
pub const fn CMSG_LEN(length: size_t) -> size_t {
3733
// See `CMSG_SPACE` impl about this sometimes being a no-op.
38-
let len = CMSG_ALIGN(size_of::<cmsghdr>()) + length;
39-
40-
len as c_uint
34+
CMSG_ALIGN(size_of::<cmsghdr>()) + length
4135
}
4236

4337
/// Total length of a padded control message for a payload of size `length`.
@@ -46,16 +40,12 @@ pub const fn CMSG_LEN(length: c_uint) -> c_uint {
4640
/// It should should *not* be used to initialize [cmsghdr::cmsg_len], given
4741
/// that the returned value includes padding bytes. Use instead [`CMSG_LEN`]
4842
/// for that.
49-
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
50-
let length = length as size_t;
51-
43+
pub const fn CMSG_SPACE(length: size_t) -> size_t {
5244
// NB: left hand side `align` is no-op when `size_of::<cmsghdr>() %
5345
// size_of::<__ALIGN_BOUNDARY>() == 0`. Such is the case on Linux, and
5446
// probably why some implementations there don't bother with the lhs
5547
// align.
56-
let space = CMSG_ALIGN(size_of::<cmsghdr>()) + CMSG_ALIGN(length);
57-
58-
space as c_uint
48+
CMSG_ALIGN(size_of::<cmsghdr>()) + CMSG_ALIGN(length)
5949
}
6050

6151
/// Returns a pointer to the payload data array associated with for the provided header.

src/new/common/solarish.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub(crate) mod sys {
1212
crate::new::common::posix::sys::socket::align_impl(p, size_of::<c_int>())
1313
}
1414

15-
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
16-
_CMSG_DATA_ALIGN(size_of::<cmsghdr>()) as c_uint + length
15+
pub const fn CMSG_LEN(length: size_t) -> size_t {
16+
_CMSG_DATA_ALIGN(size_of::<cmsghdr>()) + length
1717
}
1818

1919
pub unsafe fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {

tests/const_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#[cfg(target_os = "linux")]
2-
const _FOO: libc::c_uint = libc::CMSG_SPACE(1);
2+
const _FOO: libc::size_t = libc::CMSG_SPACE(1);
33
//^ if CMSG_SPACE is not const, this will fail to compile

0 commit comments

Comments
 (0)