rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Alexandre Courbot <acourbot@nvidia.com>
Cc: Brendan Shephard <bshephar@bne-home.net>,
	Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>,
	 dakr@kernel.org, airlied@gmail.com,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: Return Option from page_align and ensure no usize overflow
Date: Fri, 28 Nov 2025 10:09:28 +0100	[thread overview]
Message-ID: <CAH5fLggsmNd=3ezgK5A1k3YnO6ZHbJRhb0jsO0sR+ERChrewUw@mail.gmail.com> (raw)
In-Reply-To: <DEK4NFX9KS3U.38DNRG7ZMSZQJ@nvidia.com>

On Fri, Nov 28, 2025 at 7:51 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> On Fri Nov 28, 2025 at 2:29 PM JST, Brendan Shephard wrote:
> > On Fri, Nov 28, 2025 at 03:12:13AM +0100, Miguel Ojeda wrote:
> >> On Fri, Nov 28, 2025 at 1:29 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >
> >> > Ah, indeed. In that case, can we make the if arms symmetric:
> >> >
> >> >     if let Some(sum) = addr.checked_add(PAGE_SIZE - 1) {
> >> >         Some(sum & PAGE_MASK);
> >> >     } else {
> >> >         None
> >> >     }
> >>
> >> Early return style makes the happy path less indented and is more
> >> consistent with the C side. However, here the happy case would be the
> >> indented case, so `let else` would be better:
> >>
> >>     let Some(sum) = addr.checked_add(PAGE_SIZE - 1) else {
> >>         return None;
> >>     };
> >>
> >>     Some(sum & PAGE_MASK)
> >>
> >> Cheers,
> >> Miguel
> >>
> >
> > A wildcard entrant to this conversation. We could do a match instead if
> > you all might prefer that?
> >
> > match addr.checked_add(PAGE_SIZE - 1) {
> >   Some(v) => Some(v & PAGE_MASK),
> >   None => None,
> > }
>
> Clippy will likely suggest to turn this into an `if { } else { }`, which
> puts us back to where we come from. Miguel's suggestion of returning
> early in the failure path looks good to me as well.

How about using the question mark operator?

Some(addr.checked_add(PAGE_SIZE - 1)? & PAGE_MASK)

  reply	other threads:[~2025-11-28  9:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27 13:07 [PATCH] rust: Return Option from page_align and ensure no usize overflow Brendan Shephard
2025-11-27 13:42 ` Alexandre Courbot
2025-11-27 14:18   ` Brendan Shephard
2025-11-28  0:28     ` Alexandre Courbot
2025-11-28  2:12       ` Miguel Ojeda
2025-11-28  5:29         ` Brendan Shephard
2025-11-28  6:51           ` Alexandre Courbot
2025-11-28  9:09             ` Alice Ryhl [this message]
2025-11-28 13:34               ` Alexandre Courbot
2025-11-27 13:44 ` Daniel Almeida
2025-11-27 14:21   ` Brendan Shephard
2025-11-27 14:40 ` [PATCH v2] " Brendan Shephard
2025-11-27 16:24   ` Miguel Ojeda
2025-11-28  3:35     ` Brendan Shephard

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='CAH5fLggsmNd=3ezgK5A1k3YnO6ZHbJRhb0jsO0sR+ERChrewUw@mail.gmail.com' \
    --to=aliceryhl@google.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=bshephar@bne-home.net \
    --cc=dakr@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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).