Skip to content
Open
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
21 changes: 20 additions & 1 deletion src/backend/linux_raw/io/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ impl Errno {
}

/// Construct an `Errno` from a raw OS error number.
///
/// Linux error numbers are in the range `1..4096`. Values outside this
/// range are mapped to [`Errno::INVAL`].
#[inline]
pub const fn from_raw_os_error(raw: i32) -> Self {
Self::from_errno(raw as u32)
Self::from_errno(if raw < 1 || raw >= 4096 {
errno::EINVAL
} else {
raw as u32
})
}

/// Convert from a C `errno` value (which is positive) to an `Errno`.
Expand Down Expand Up @@ -550,3 +557,15 @@ impl Errno {
/// `EXFULL`
pub const XFULL: Self = Self::from_errno(errno::EXFULL);
}

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

#[test]
fn test_error_out_of_range() {
for raw in [0, 4096, -1, -22, i32::MIN, i32::MAX, 0x1_0001, 0x1_0016] {
assert_eq!(Errno::from_raw_os_error(raw), Errno::INVAL);
}
}
}
Loading