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 895a4b662..2d15c7be3 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 + /// 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::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); + } + } +}