Skip to content

Commit 77968ec

Browse files
committed
netbsd: fix fallback and test it using configuration flag
1 parent 584926e commit 77968ec

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ jobs:
208208
usesh: true
209209
prepare: |
210210
/usr/sbin/pkg_add rust
211-
run: cargo test
211+
run: |
212+
cargo test
213+
RUSTFLAGS="--cfg getrandom_test_netbsd_fallback -D warnings" cargo test
212214
213215
# This job currently fails:
214216
# https://github.com/rust-random/getrandom/actions/runs/11405005618/job/31735653874?pr=528

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ check-cfg = [
8383
'cfg(getrandom_sanitize)',
8484
'cfg(getrandom_browser_test)',
8585
'cfg(getrandom_test_linux_fallback)',
86+
'cfg(getrandom_test_netbsd_fallback)',
8687
]
8788

8889
[package.metadata.docs.rs]

src/backends/netbsd.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@ unsafe extern "C" fn polyfill_using_kern_arand(
3030
// NetBSD will only return up to 256 bytes at a time, and
3131
// older NetBSD kernels will fail on longer buffers.
3232
let mut len = cmp::min(buflen, 256);
33-
let expected_ret = libc::c_int::try_from(len).expect("len is bounded by 256");
34-
3533
let ret = unsafe { libc::sysctl(MIB.as_ptr(), MIB_LEN, buf, &mut len, ptr::null(), 0) };
3634

37-
if ret == expected_ret {
38-
libc::ssize_t::try_from(ret).expect("len is bounded by 256")
39-
} else if ret == -1 {
40-
-1
41-
} else {
35+
match ret {
36+
0 if len <= 256 => {
37+
libc::ssize_t::try_from(len).expect("len is unsigned and smaller than 256")
38+
}
39+
-1 => -1,
4240
// Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact`
43-
0
41+
_ => 0,
4442
}
4543
}
4644

@@ -53,7 +51,7 @@ fn init() -> *mut c_void {
5351
static NAME: &[u8] = b"getrandom\0";
5452
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
5553
let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
56-
if ptr.is_null() {
54+
if ptr.is_null() || cfg!(getrandom_test_netbsd_fallback) {
5755
// Verify `polyfill_using_kern_arand` has the right signature.
5856
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
5957
ptr = POLYFILL as *mut c_void;

0 commit comments

Comments
 (0)