rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Alice Ryhl <aliceryhl@google.com>
Cc: rust-for-linux@vger.kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
	tamird@gmail.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: uaccess: use to_result for error handling
Date: Fri, 22 Aug 2025 08:02:52 +0300	[thread overview]
Message-ID: <20250822080252.773d6f54@nimda.home> (raw)
In-Reply-To: <CAH5fLggt4YJe93xo9KTr=hTQoj28=jjJtaxo=gFmnTbWmm8SRg@mail.gmail.com>

On Thu, 21 Aug 2025 13:04:18 +0200
Alice Ryhl <aliceryhl@google.com> wrote:

> On Thu, Aug 21, 2025 at 11:19 AM Onur Özkan <work@onurozkan.dev>
> wrote:
> >
> > Simplifies error handling by replacing the manual check
> > of the return value with the `to_result` helper.
> >
> > Signed-off-by: Onur Özkan <work@onurozkan.dev>
> > ---
> >  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

Nice catch. I could use `try_into().unwrap_or(0)` but that feels a bit
iffy since it would silently avoid the error if `isize` happens to be
much smaller than what `i32` can handle (though I am not sure if it's
possible in practice in the kernel codebase). Let's just ignore this
patch.

Thanks,
Onur

  reply	other threads:[~2025-08-22  5:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21  9:19 [PATCH] rust: uaccess: use to_result for error handling Onur Özkan
2025-08-21 11:04 ` Alice Ryhl
2025-08-22  5:02   ` Onur Özkan [this message]
2025-08-22 20:24     ` Miguel Ojeda
2025-08-25 22:57 ` Elle Rhumsaa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250822080252.773d6f54@nimda.home \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).