Skip to content

Commit cee3bf1

Browse files
authored
netbsd: fix fallback and test it using configuration flag (#555)
Fixes bug in the NetBSD fallback code based on `KERN_ARND`. `sysctl` returns either 0 or -1, while the code assumed that on success it returns number of bytes filled. Adds the `getrandom_test_netbsd_fallback` configuration flag, which forces the NetBSD backend to use the `KERN_ARND` fallback, and tweaks CI to use the flag.
1 parent 584926e commit cee3bf1

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

.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

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
- Memory sanitizer support gated behind `getrandom_sanitize` configuration flag [#521]
3939
- `u32` and `u64` functions for generating random values of the respective type [#544]
4040

41+
### Fixed
42+
- NetBSD fallback code based on `KERN_ARND` [#555]
43+
4144
[#415]: https://github.com/rust-random/getrandom/pull/415
4245
[#440]: https://github.com/rust-random/getrandom/pull/440
4346
[#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
5457
[#542]: https://github.com/rust-random/getrandom/pull/542
5558
[#544]: https://github.com/rust-random/getrandom/pull/544
5659
[#554]: https://github.com/rust-random/getrandom/pull/554
60+
[#555]: https://github.com/rust-random/getrandom/pull/555
5761

5862
## [0.2.15] - 2024-05-06
5963
### Added

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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ 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 => libc::ssize_t::try_from(len).expect("len is in the range of 0..=256"),
37+
-1 => -1,
4238
// Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact`
43-
0
39+
_ => 0,
4440
}
4541
}
4642

@@ -53,7 +49,7 @@ fn init() -> *mut c_void {
5349
static NAME: &[u8] = b"getrandom\0";
5450
let name_ptr = NAME.as_ptr().cast::<libc::c_char>();
5551
let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) };
56-
if ptr.is_null() {
52+
if ptr.is_null() || cfg!(getrandom_test_netbsd_fallback) {
5753
// Verify `polyfill_using_kern_arand` has the right signature.
5854
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
5955
ptr = POLYFILL as *mut c_void;

0 commit comments

Comments
 (0)