Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/backend/libc/io/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,11 +1103,27 @@ impl Errno {
}

/// Construct an `Errno` from a raw OS error number.
///
/// # Panics
///
/// Panics if the value of `raw` is outside the range of values that
/// the OS returns. Use `try_from_raw_os_error` for explicit error handling.
#[inline]
pub const fn from_raw_os_error(raw: i32) -> Self {
Self(raw)
}

/// Construct an `Errno` from a raw OS error number.
///
/// Returns `None` if the value is outside the range of values that the OS
/// returns.
#[inline]
pub const fn try_from_raw_os_error(raw: i32) -> Option<Self> {
// Unlike the linux_raw backend, we don't know the range of errno
// values that libc might use, so we assume any value is valid.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know zero is not valid:

No function in this volume of POSIX.1-2024 shall set errno to 0.

https://pubs.opengroup.org/onlinepubs/9799919799/functions/errno.html

Similarly, C spec:

library functions never store 0 in errno

https://en.cppreference.com/c/error/errno

Some(Self(raw))
}

pub(crate) fn last_os_error() -> Self {
Self(errno().0)
}
Expand Down
58 changes: 50 additions & 8 deletions src/backend/linux_raw/io/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ impl Errno {
io_err.raw_os_error().and_then(|raw| {
// `std::io::Error` could theoretically have arbitrary OS error
// values, so check that they're in Linux's range.
if (1..4096).contains(&raw) {
Some(Self::from_errno(raw as u32))
} else {
None
}
Self::try_from_errno(raw as u32)
})
}

Expand All @@ -76,24 +72,53 @@ impl Errno {
}

/// Construct an `Errno` from a raw OS error number.
///
/// # Panics
///
/// Panics if the value of `raw` is outside the range of values that
/// the OS returns. Use `try_from_raw_os_error` for explicit error handling.
#[inline]
pub const fn from_raw_os_error(raw: i32) -> Self {
Self::from_errno(raw as u32)
}

/// Construct an `Errno` from a value that might be a raw OS error number.
///
/// Returns `None` if the value is outside the range of values that the OS
/// returns.
#[inline]
pub fn try_from_raw_os_error(raw: i32) -> Option<Self> {
Self::try_from_errno(raw as u32)
}

/// Convert from a C `errno` value (which is positive) to an `Errno`.
const fn from_errno(raw: u32) -> Self {
// TODO: Use Range::contains, once that's `const`.
assert!(raw != 0 && raw < 4096);

// We store error values in negated form, so that we don't have to
// negate them after every syscall.
let encoded = raw.wrapping_neg() as u16;

// TODO: Use Range::contains, once that's `const`.
assert!(encoded >= 0xf001);

// SAFETY: Linux syscalls return negated error values in the range
// `-4095..0`, which we just asserted.
unsafe { Self(encoded) }
}

/// Convert from a C `errno` value (which is positive) to an `Errno`.
fn try_from_errno(raw: u32) -> Option<Self> {
if raw == 0 || raw >= 4096 {
return None;
}

// We store error values in negated form, so that we don't have to
// negate them after every syscall.
let encoded = raw.wrapping_neg() as u16;

// SAFETY: Linux syscalls return negated error values in the range
// `-4095..0`, which we just checked.
Some(unsafe { Self(encoded) })
}
}

/// Check for an error from the result of a syscall which encodes a
Expand Down Expand Up @@ -550,3 +575,20 @@ impl Errno {
/// `EXFULL`
pub const XFULL: Self = Self::from_errno(errno::EXFULL);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_errno_out_of_range() {
for raw in [0, 4096, -1, -22, i32::MIN, i32::MAX, 0x1_0001, 0x1_0016] {
assert_eq!(Errno::try_from_raw_os_error(raw), None);
assert!(std::panic::catch_unwind(|| Errno::from_raw_os_error(raw)).is_err());
}
for raw in [1, 4095, 256, 1023, 1024] {
assert!(Errno::try_from_raw_os_error(raw).is_some());
let _ok = Errno::from_raw_os_error(raw);
}
}
}
Loading