Skip to content

Commit 4b58c5e

Browse files
committed
netbsd: Clarify type conversions are lossless.
1 parent 9e79890 commit 4b58c5e

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/netbsd.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ 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+
let mut len = core::cmp::min(buf.len(), libc::ssize_t::MAX.unsignd_abs());
8+
let ret: libc::c_int = unsafe {
99
libc::sysctl(
1010
MIB.as_ptr(),
11+
#[allow(clippy::cast_possible_truncation)]
1112
MIB.len() as libc::c_uint,
1213
buf.as_mut_ptr().cast::<c_void>(),
1314
&mut len,
@@ -18,7 +19,13 @@ fn kern_arnd(buf: &mut [MaybeUninit<u8>]) -> libc::ssize_t {
1819
if ret == -1 {
1920
-1
2021
} else {
21-
len as libc::ssize_t
22+
// c_int to ssize_t conversion is lossless.
23+
const _: () =
24+
assert!(core::mem::size_of::<libc::c_int>() == core::mem::size_of::<libc::ssize_t>());
25+
// We clamped the request to `ssize_t::MAX` bytes so this lossless.
26+
#[allow(clippy::cast_possible_truncation)]
27+
let len = len as libc::ssize_t;
28+
len
2229
}
2330
}
2431

0 commit comments

Comments
 (0)