Skip to contentSkip to conversation

rust: error: modify `from_errno` to use `try_from_errno`

PATCH Applied Daniel Sedlak 3 participants 3 messages Dec 7, 2024 – Dec 17, 2024 Rust

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 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.

---
v3 -> v4:
- Now everything should be rebased correctly.
- Link to previous version [4]

v1 -> v3:

Currently, there are three patches for this issue. There was a race condition between me [1] and Guilherme [2] because we both picked it up as a "good first issue" without knowing that one of us had started working on it. We both agreed that one of us would be the main author and the other one would be noted in "Codeveloped-by." However, Guilherme has kept radio silence since [3]. So, I am resubmitting the patch, which builds on top of [3].

 rust/kernel/error.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
rust/kernel/error.rs +4 -7
index 52c502432447..9b508f10c211 100644
@@ -101,19 +101,16 @@ impl Error {
     /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
     /// be returned in such a case.
     pub fn from_errno(errno: crate::ffi::c_int) -> Error {
-        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
+        if let Some(error) = Self::try_from_errno(errno) {
+            error
+        } else {
             // TODO: Make it a `WARN_ONCE` once available.
             crate::pr_warn!(
                 "attempted to create `Error` with out of range `errno`: {}",
                 errno
             );
-            return code::EINVAL;
+            code::EINVAL
         }
-
-        // INVARIANT: The check above ensures the type invariant
-        // will hold.
-        // SAFETY: `errno` is checked above to be in a valid range.
-        unsafe { Error::from_errno_unchecked(errno) }
     }
 
     /// Creates an [`Error`] from a kernel error code.

---
base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37

Daniel

Signature · 1 line
2.47.1
Reviewed-by on v4 1/1
#1 lore raw
Quoted 9 lines · Daniel Sedlak

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.

Quoted 57 lines

---
v3 -> v4:
- Now everything should be rebased correctly.
- Link to previous version [4]

v1 -> v3:

Currently, there are three patches for this issue. There was a race condition between me [1] and Guilherme [2] because we both picked it up as a "good first issue" without knowing that one of us had started working on it. We both agreed that one of us would be the main author and the other one would be noted in "Codeveloped-by." However, Guilherme has kept radio silence since [3]. So, I am resubmitting the patch, which builds on top of [3].

 rust/kernel/error.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
rust/kernel/error.rs +4 -8
index 52c502432447..9b508f10c211 100644
@@ -101,19 +101,16 @@ impl Error {
     /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
     /// be returned in such a case.
     pub fn from_errno(errno: crate::ffi::c_int) -> Error {
-        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
+        if let Some(error) = Self::try_from_errno(errno) {
+            error
+        } else {
             // TODO: Make it a `WARN_ONCE` once available.
             crate::pr_warn!(
                 "attempted to create `Error` with out of range `errno`: {}",
                 errno
             );
-            return code::EINVAL;
+            code::EINVAL
         }
-
-        // INVARIANT: The check above ensures the type invariant
-        // will hold.
-        // SAFETY: `errno` is checked above to be in a valid range.
-        unsafe { Error::from_errno_unchecked(errno) }
     }
 
     /// Creates an [`Error`] from a kernel error code.
 
---

base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37

Daniel

Signature · 1 line
2.47.1
Applied
#2 lore raw
In reply to Daniel Sedlak
Quoted 10 lines · Daniel Sedlak

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.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel