From 25b64bb9781a07ee5a941780f7b601a8a0afb654 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 12 Nov 2026 10:39:45 -0800 Subject: [PATCH 1/2] Add a non-panic API for converting raw integers to `Errno`. Add an `Errno::try_from_raw_os_error` function, as a non-panicking version of `Errno::from_raw_os_error`. While here, fix the range check in `Errno::from_raw_os_error` to correctly check the full range of disallowed values. --- src/backend/linux_raw/io/errno.rs | 58 ++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/src/backend/linux_raw/io/errno.rs b/src/backend/linux_raw/io/errno.rs index 895a4b662..6a1de6449 100644 --- a/src/backend/linux_raw/io/errno.rs +++ b/src/backend/linux_raw/io/errno.rs @@ -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) }) } @@ -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 + /// Linux 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 Linux + /// returns. + #[inline] + pub fn try_from_raw_os_error(raw: i32) -> Option { + 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 { + 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 @@ -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); + } + } +} From 017a1053ac6b32b81a9ca9a621b972d0c7bc8406 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 12 Nov 2026 14:09:12 -0800 Subject: [PATCH 2/2] Add the libc version of the API too. --- src/backend/libc/io/errno.rs | 16 ++++++++++++++++ src/backend/linux_raw/io/errno.rs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/backend/libc/io/errno.rs b/src/backend/libc/io/errno.rs index 81f0e4893..e9d397036 100644 --- a/src/backend/libc/io/errno.rs +++ b/src/backend/libc/io/errno.rs @@ -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 { + // 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. + Some(Self(raw)) + } + pub(crate) fn last_os_error() -> Self { Self(errno().0) } diff --git a/src/backend/linux_raw/io/errno.rs b/src/backend/linux_raw/io/errno.rs index 6a1de6449..2d15c7be3 100644 --- a/src/backend/linux_raw/io/errno.rs +++ b/src/backend/linux_raw/io/errno.rs @@ -76,7 +76,7 @@ impl Errno { /// # Panics /// /// Panics if the value of `raw` is outside the range of values that - /// Linux returns. Use `try_from_raw_os_error` for explicit error handling. + /// 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) @@ -84,7 +84,7 @@ impl Errno { /// 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 Linux + /// 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 {