Coverage Report

Created: 2025-08-28 06:34

/rust/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.3.3/src/util_libc.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::Error;
2
use core::mem::MaybeUninit;
3
4
cfg_if! {
5
    if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android", target_os = "cygwin"))] {
6
        use libc::__errno as errno_location;
7
    } else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
8
        use libc::__errno_location as errno_location;
9
    } else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
10
        use libc::___errno as errno_location;
11
    } else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
12
        use libc::__error as errno_location;
13
    } else if #[cfg(target_os = "haiku")] {
14
        use libc::_errnop as errno_location;
15
    } else if #[cfg(target_os = "nto")] {
16
        use libc::__get_errno_ptr as errno_location;
17
    } else if #[cfg(any(all(target_os = "horizon", target_arch = "arm"), target_os = "vita"))] {
18
        extern "C" {
19
            // Not provided by libc: https://github.com/rust-lang/libc/issues/1995
20
            fn __errno() -> *mut libc::c_int;
21
        }
22
        use __errno as errno_location;
23
    } else if #[cfg(target_os = "aix")] {
24
        use libc::_Errno as errno_location;
25
    }
26
}
27
28
cfg_if! {
29
    if #[cfg(target_os = "vxworks")] {
30
        use libc::errnoGet as get_errno;
31
    } else {
32
0
        unsafe fn get_errno() -> libc::c_int { *errno_location() }
33
    }
34
}
35
36
0
pub(crate) fn last_os_error() -> Error {
37
0
    // We assume that on all targets which use the `util_libc` module `c_int` is equal to `i32`
38
0
    let errno: i32 = unsafe { get_errno() };
39
0
40
0
    if errno > 0 {
41
0
        let code = errno
42
0
            .checked_neg()
43
0
            .expect("Positive number can be always negated");
44
0
        Error::from_neg_error_code(code)
45
    } else {
46
0
        Error::ERRNO_NOT_POSITIVE
47
    }
48
0
}
49
50
/// Fill a buffer by repeatedly invoking `sys_fill`.
51
///
52
/// The `sys_fill` function:
53
///   - should return -1 and set errno on failure
54
///   - should return the number of bytes written on success
55
#[allow(dead_code)]
56
0
pub(crate) fn sys_fill_exact(
57
0
    mut buf: &mut [MaybeUninit<u8>],
58
0
    sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
59
0
) -> Result<(), Error> {
60
0
    while !buf.is_empty() {
61
0
        let res = sys_fill(buf);
62
0
        match res {
63
0
            res if res > 0 => {
64
0
                let len = usize::try_from(res).map_err(|_| Error::UNEXPECTED)?;
Unexecuted instantiation: getrandom::backends::use_file::util_libc::sys_fill_exact::<getrandom::backends::linux_android_with_fallback::fill_inner::{closure#0}>::{closure#0}
Unexecuted instantiation: getrandom::backends::use_file::util_libc::sys_fill_exact::<getrandom::backends::use_file::fill_inner::{closure#0}>::{closure#0}
65
0
                buf = buf.get_mut(len..).ok_or(Error::UNEXPECTED)?;
66
            }
67
            -1 => {
68
0
                let err = last_os_error();
69
0
                // We should try again if the call was interrupted.
70
0
                if err.raw_os_error() != Some(libc::EINTR) {
71
0
                    return Err(err);
72
0
                }
73
            }
74
            // Negative return codes not equal to -1 should be impossible.
75
            // EOF (ret = 0) should be impossible, as the data we are reading
76
            // should be an infinite stream of random bytes.
77
0
            _ => return Err(Error::UNEXPECTED),
78
        }
79
    }
80
0
    Ok(())
81
0
}
Unexecuted instantiation: getrandom::backends::use_file::util_libc::sys_fill_exact::<getrandom::backends::linux_android_with_fallback::fill_inner::{closure#0}>
Unexecuted instantiation: getrandom::backends::use_file::util_libc::sys_fill_exact::<getrandom::backends::use_file::fill_inner::{closure#0}>