diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e96ad130d..6e92dae04 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -208,7 +208,9 @@ jobs: usesh: true prepare: | /usr/sbin/pkg_add rust - run: cargo test + run: | + cargo test + RUSTFLAGS="--cfg getrandom_test_netbsd_fallback -D warnings" cargo test # This job currently fails: # https://github.com/rust-random/getrandom/actions/runs/11405005618/job/31735653874?pr=528 diff --git a/CHANGELOG.md b/CHANGELOG.md index 01de288e6..ce4cebbc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Memory sanitizer support gated behind `getrandom_sanitize` configuration flag [#521] - `u32` and `u64` functions for generating random values of the respective type [#544] +### Fixed +- NetBSD fallback code based on `KERN_ARND` [#555] + [#415]: https://github.com/rust-random/getrandom/pull/415 [#440]: https://github.com/rust-random/getrandom/pull/440 [#442]: https://github.com/rust-random/getrandom/pull/442 @@ -54,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#542]: https://github.com/rust-random/getrandom/pull/542 [#544]: https://github.com/rust-random/getrandom/pull/544 [#554]: https://github.com/rust-random/getrandom/pull/554 +[#555]: https://github.com/rust-random/getrandom/pull/555 ## [0.2.15] - 2024-05-06 ### Added diff --git a/Cargo.toml b/Cargo.toml index 16e47a4ba..28b48a64c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ check-cfg = [ 'cfg(getrandom_sanitize)', 'cfg(getrandom_browser_test)', 'cfg(getrandom_test_linux_fallback)', + 'cfg(getrandom_test_netbsd_fallback)', ] [package.metadata.docs.rs] diff --git a/src/backends/netbsd.rs b/src/backends/netbsd.rs index 0e3268ef4..57fa91a00 100644 --- a/src/backends/netbsd.rs +++ b/src/backends/netbsd.rs @@ -30,17 +30,13 @@ unsafe extern "C" fn polyfill_using_kern_arand( // NetBSD will only return up to 256 bytes at a time, and // older NetBSD kernels will fail on longer buffers. let mut len = cmp::min(buflen, 256); - let expected_ret = libc::c_int::try_from(len).expect("len is bounded by 256"); - let ret = unsafe { libc::sysctl(MIB.as_ptr(), MIB_LEN, buf, &mut len, ptr::null(), 0) }; - if ret == expected_ret { - libc::ssize_t::try_from(ret).expect("len is bounded by 256") - } else if ret == -1 { - -1 - } else { + match ret { + 0 if len <= 256 => libc::ssize_t::try_from(len).expect("len is in the range of 0..=256"), + -1 => -1, // Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact` - 0 + _ => 0, } } @@ -53,7 +49,7 @@ fn init() -> *mut c_void { static NAME: &[u8] = b"getrandom\0"; let name_ptr = NAME.as_ptr().cast::(); let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }; - if ptr.is_null() { + if ptr.is_null() || cfg!(getrandom_test_netbsd_fallback) { // Verify `polyfill_using_kern_arand` has the right signature. const POLYFILL: GetRandomFn = polyfill_using_kern_arand; ptr = POLYFILL as *mut c_void;