Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: https://github.com/Rust-for-Linux/linux/issues/1125
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Co-developed-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Signed-off-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Link: https://lore.kernel.org/r/20241207112445.55502-1-daniel@sedlak.dev
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Daniel Sedlak and committed by
Miguel Ojeda
9a02cbc5 9b98be76

+4 -7
+4 -7
rust/kernel/error.rs
··· 101 101 /// It is a bug to pass an out-of-range `errno`. `EINVAL` would 102 102 /// be returned in such a case. 103 103 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 { 105 107 // TODO: Make it a `WARN_ONCE` once available. 106 108 crate::pr_warn!( 107 109 "attempted to create `Error` with out of range `errno`: {}", 108 110 errno 109 111 ); 110 - return code::EINVAL; 112 + code::EINVAL 111 113 } 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) } 117 114 } 118 115 119 116 /// Creates an [`Error`] from a kernel error code.