Re: [PATCH] rust: uaccess: use to_result for error handling
From: Alice Ryhl
Date: Thu Aug 21 2025 - 07:05:52 EST
On Thu, Aug 21, 2025 at 11:19 AM Onur Özkan <work@xxxxxxxxxxxxx> wrote:
>
> Simplifies error handling by replacing the manual check
> of the return value with the `to_result` helper.
>
> Signed-off-by: Onur Özkan <work@xxxxxxxxxxxxx>
> ---
> rust/kernel/uaccess.rs | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> index a8fb4764185a..9992eece2694 100644
> --- a/rust/kernel/uaccess.rs
> +++ b/rust/kernel/uaccess.rs
> @@ -7,7 +7,7 @@
> use crate::{
> alloc::{Allocator, Flags},
> bindings,
> - error::Result,
> + error::{to_result, Result},
> ffi::{c_char, c_void},
> prelude::*,
> transmute::{AsBytes, FromBytes},
> @@ -495,9 +495,7 @@ fn raw_strncpy_from_user(dst: &mut [MaybeUninit<u8>], src: UserPtr) -> Result<us
> )
> };
>
> - if res < 0 {
> - return Err(Error::from_errno(res as i32));
> - }
> + to_result(res as i32)?;
This is wrong. The type of `res` is isize, and casting a positive
isize to i32 can result in a negative number and incorrectly trigger
this error. For example: 2147483650i64 as i32 is -2147483646.
Alice