All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] rust: error: modify `from_errno` to use `try_from_errno`
@ 2024-12-07 11:24 Daniel Sedlak
  2024-12-10 10:13 ` Fiona Behrens
  2024-12-17 23:24 ` Miguel Ojeda
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Sedlak @ 2024-12-07 11:24 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Guilherme Augusto Martins da Silva, 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.

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>
---
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]. 

Link: https://lore.kernel.org/rust-for-linux/20241104185135.18974-1-daniel@sedlak.dev/ [1]
Link: https://lore.kernel.org/rust-for-linux/20241104200014.12996-1-guilhermev2huehue@gmail.com/ [2]
Link: https://lore.kernel.org/rust-for-linux/20241105114819.14051-1-guilhermev2huehue@gmail.com/ [3]
Link: https://lore.kernel.org/rust-for-linux/20241123104857.49385-1-daniel@sedlak.dev/ [4]

 rust/kernel/error.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 52c502432447..9b508f10c211 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -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
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-17 23:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-07 11:24 [PATCH v4] rust: error: modify `from_errno` to use `try_from_errno` Daniel Sedlak
2024-12-10 10:13 ` Fiona Behrens
2024-12-17 23:24 ` Miguel Ojeda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.