Skip to content

Commit 3f7cbe8

Browse files
wutchzonejannau
authored andcommitted
rust: error: modify from_errno to use try_from_errno
Modify the from_errno function to use try_from_errno to reduce code duplication while still maintaining all existing behavior and error handling and also reduces unsafe code. Link: Rust-for-Linux#1125 Suggested-by: Miguel Ojeda <[email protected]> Co-developed-by: Guilherme Augusto Martins da Silva <[email protected]> Signed-off-by: Guilherme Augusto Martins da Silva <[email protected]> Signed-off-by: Daniel Sedlak <[email protected]> Reviewed-by: Fiona Behrens <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent c5e32b0 commit 3f7cbe8

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

rust/kernel/error.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,16 @@ impl Error {
101101
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
102102
/// be returned in such a case.
103103
pub fn from_errno(errno: crate::ffi::c_int) -> Error {
104-
if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
104+
if let Some(error) = Self::try_from_errno(errno) {
105+
error
106+
} else {
105107
// TODO: Make it a `WARN_ONCE` once available.
106108
crate::pr_warn!(
107109
"attempted to create `Error` with out of range `errno`: {}",
108110
errno
109111
);
110-
return code::EINVAL;
112+
code::EINVAL
111113
}
112-
113-
// INVARIANT: The check above ensures the type invariant
114-
// will hold.
115-
// SAFETY: `errno` is checked above to be in a valid range.
116-
unsafe { Error::from_errno_unchecked(errno) }
117114
}
118115

119116
/// Creates an [`Error`] from a kernel error code.

0 commit comments

Comments
 (0)