Rust for Linux List
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Lorenzo Delgado" <lnsdev@proton.me>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Thorsten Blum" <blum@kernel.org>,
	"Francis Laniel" <laniel_francis@npnm.eu>
Subject: Re: [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf()
Date: Thu, 15 Oct 2026 22:06:32 +0900	[thread overview]
Message-ID: <DM5FNHG3HA25.LXCECF3U879T@nvidia.com> (raw)
In-Reply-To: <20261013-nonempty-slice-v3-2-4463cc0338f8@proton.me>

On Wed Oct 14, 2026 at 5:30 PM JST, Lorenzo Delgado wrote:
<...>
> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> index 5f6c4d7a1a51..1cc5c7580d3a 100644
> --- a/rust/kernel/uaccess.rs
> +++ b/rust/kernel/uaccess.rs
> @@ -11,6 +11,7 @@
>      error::Result,
>      ffi::{c_char, c_void},
>      fs::file,
> +    non_empty::NonEmpty,
>      prelude::*,
>      ptr::KnownSize,
>      transmute::{AsBytes, FromBytes},
> @@ -383,20 +384,16 @@ pub fn read_all<A: Allocator>(mut self, buf: &mut Vec<u8, A>, flags: Flags) -> R
>      /// Read a NUL-terminated string from userspace and return it.
>      ///
>      /// The string is read into `buf` and a NUL-terminator is added if the end of `buf` is reached.
> -    /// Since there must be space to add a NUL-terminator, the buffer must not be empty. The
> +    /// Since there must be space to add a NUL-terminator, `buf` is a [`NonEmpty`] slice. The
>      /// returned `&CStr` points into `buf`.
>      ///
>      /// Fails with [`EFAULT`] if the read happens on a bad address (some data may have been
>      /// copied).
>      #[doc(alias = "strncpy_from_user")]
> -    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut [u8]) -> Result<&'buf CStr> {
> -        if buf.is_empty() {
> -            return Err(EINVAL);
> -        }
> -
> +    pub fn strcpy_into_buf<'buf>(self, buf: &'buf mut NonEmpty<[u8]>) -> Result<&'buf CStr> {

I understand this was done per the v1 feedback, but won't it make it
more tedious for users to call this method? (hard to tell as there are
no users at the moment :))

We could still benefit from `NonEmpty` if we replace the `buf.is_empty`
by an attempt to build it and return `EINVAL` if that fails (what v1 did
basically) and calling `last_mut` on it to remove the
`unwrap_unchecked`.

Of course if callers are expected to already work with a `NonEmpty` then
that's fine, but otherwise my concern is that every one of them would
have to manually build one to perform a check that this method can
easily do.

  reply	other threads:[~2026-10-15 13:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  8:30 [PATCH v3 0/2] rust: add NonEmpty Lorenzo Delgado
2026-10-14  8:30 ` [PATCH v3 1/2] rust: non_empty: " Lorenzo Delgado
2026-10-14 11:55   ` Miguel Ojeda
2026-10-14 14:25     ` Gary Guo
2026-10-15 12:43       ` Alexandre Courbot
2026-10-14  8:30 ` [PATCH v3 2/2] rust: uaccess: use NonEmpty in strcpy_into_buf() Lorenzo Delgado
2026-10-15 13:06   ` Alexandre Courbot [this message]
2026-10-15 12:57 ` [PATCH v3 0/2] rust: add NonEmpty Alexandre Courbot

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=DM5FNHG3HA25.LXCECF3U879T@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=blum@kernel.org \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=laniel_francis@npnm.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lnsdev@proton.me \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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