Skip to content

Commit 2fe1763

Browse files
committed
netbsd: Clarify type conversions are lossless.
1 parent 86b6f50 commit 2fe1763

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/netbsd.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ use core::{ffi::c_void, mem::MaybeUninit, ptr};
44

55
fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
66
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
7-
let mut len = buf.len();
8-
let ret = unsafe {
7+
#[allow(clippy::cast_possible_truncation)]
8+
let mib_len = MIB.len() as libc::c_uint;
9+
10+
let mut len = core::cmp::min(buf.len(), libc::ssize_t::MAX.unsignd_abs());
11+
let ret: libc::c_int = unsafe {
912
libc::sysctl(
1013
MIB.as_ptr(),
11-
MIB.len() as libc::c_uint,
14+
mib_len,
1215
buf.as_mut_ptr().cast::<c_void>(),
1316
&mut len,
1417
ptr::null(),
@@ -18,7 +21,13 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
1821
if ret == -1 {
1922
-1
2023
} else {
21-
len as libc::ssize_t
24+
// c_int to ssize_t conversion is lossless.
25+
const _: () =
26+
assert!(core::mem::size_of::<libc::c_int>() == core::mem::size_of::<libc::ssize_t>());
27+
// We clamped the request to `ssize_t::MAX` bytes so this lossless.
28+
#[allow(clippy::cast_possible_truncation)]
29+
let len = len as libc::ssize_t;
30+
len
2231
}
2332
}
2433

0 commit comments

Comments
 (0)