From: Alice Ryhl <aliceryhl@google.com>
To: Tamir Duberstein <tamird@gmail.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Andrew Ballance" <andrewjballance@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 3/4] rust: alloc: refactor `Vec::truncate` using `dec_len`
Date: Wed, 19 Mar 2025 09:53:09 +0000 [thread overview]
Message-ID: <Z9qUBe_8SM4c-9UI@google.com> (raw)
In-Reply-To: <20250318-vec-set-len-v2-3-293d55f82d18@gmail.com>
On Tue, Mar 18, 2025 at 04:13:55PM -0400, Tamir Duberstein wrote:
> Use `checked_sub` to satisfy the safety requirements of `dec_len` and
> replace nearly the whole body of `truncate` with a call to `dec_len`.
>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> rust/kernel/alloc/kvec.rs | 29 +++++++++++------------------
> 1 file changed, 11 insertions(+), 18 deletions(-)
>
> diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
> index 97cc5ab11e2a..6f4dc89ef7f8 100644
> --- a/rust/kernel/alloc/kvec.rs
> +++ b/rust/kernel/alloc/kvec.rs
> @@ -489,25 +489,18 @@ pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocEr
> /// # Ok::<(), Error>(())
> /// ```
> pub fn truncate(&mut self, len: usize) {
> - if len >= self.len() {
> - return;
> + match self.len().checked_sub(len) {
> + None => {}
> + Some(count) => {
This could be simplified as:
if let Some(count) = self.len().checked_sub(len) {
// logic here
}
or
let Some(count) = self.len().checked_sub(len) else {
return;
}
// logic here
> + // SAFETY: `count` is `self.len() - len` so it is guaranteed to be less than or
> + // equal to `self.len()`.
> + let tail = unsafe { self.dec_len(count) };
> +
> + // SAFETY: the contract of `dec_len` guarantees that the elements in `tail` are
> + // valid elements whose ownership has been transferred to the caller.
> + unsafe { ptr::drop_in_place(ptr) };
We have a mutable reference to these elements until after the
`drop_in_place` call, but the elements are invalidated by that call.
This means that we have a mutable reference to invalid values, which
violates the invariants for mutable references.
Consider converting to a raw pointer when creating `tail` instead to
avoid that:
let tail: *mut [T] = unsafe { self.dec_len(count) };
unsafe { ptr::drop_in_place(ptr) };
Alice
next prev parent reply other threads:[~2025-03-19 9:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-18 20:13 [PATCH v2 0/4] rust: alloc: split `Vec::set_len` into `Vec::{inc,dec}_len` Tamir Duberstein
2025-03-18 20:13 ` [PATCH v2 1/4] rust: alloc: add Vec::len() <= Vec::capacity invariant Tamir Duberstein
2025-03-18 20:13 ` [PATCH v2 2/4] rust: alloc: add `Vec::dec_len` Tamir Duberstein
2025-03-18 20:13 ` [PATCH v2 3/4] rust: alloc: refactor `Vec::truncate` using `dec_len` Tamir Duberstein
2025-03-18 20:16 ` Tamir Duberstein
2025-03-19 9:53 ` Alice Ryhl [this message]
2025-03-19 13:40 ` Tamir Duberstein
2025-03-18 20:13 ` [PATCH v2 4/4] rust: alloc: replace `Vec::set_len` with `inc_len` Tamir Duberstein
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=Z9qUBe_8SM4c-9UI@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=andrewjballance@gmail.com \
--cc=benno.lossin@proton.me \
--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=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 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.