public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Albab Hasan <albabhasan276@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: ojeda@kernel.org, acourbot@nvidia.com,
	linux-kernel@vger.kernel.org,
	Albab Hasan <albabhasan276@gmail.com>
Subject: [PATCH] rust: transmute: use split_at_checked() and split_at_mut_checked()
Date: Tue, 10 Mar 2026 15:57:10 +0600	[thread overview]
Message-ID: <20260310095710.1166-1-albabhasan276@gmail.com> (raw)

Replace manual bounds checking followed by split_at() and split_at_mut()
calls with the checked variants split_at_checked() and
split_at_mut_checked(), which return None instead of panicking on
out-of-bounds indices.

These methods were stabilized in Rust 1.80.0, which is the current
minimum supported Rust version for the kernel.

This simplifies from_bytes_prefix(), from_bytes_mut_prefix(), and
from_bytes_copy_prefix() by removing the explicit bounds checks and
panic-avoidance comments that are no longer needed.

Signed-off-by: Albab Hasan <albabhasan276@gmail.com>
---
 rust/kernel/transmute.rs | 33 ++++++---------------------------
 1 file changed, 6 insertions(+), 27 deletions(-)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index 5711580c9f9b..643b19406a24 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -67,16 +67,9 @@ fn from_bytes_prefix(bytes: &[u8]) -> Option<(&Self, &[u8])>
     where
         Self: Sized,
     {
-        if bytes.len() < size_of::<Self>() {
-            None
-        } else {
-            // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
-            // panic.
-            // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
-            let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+        let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
 
-            Self::from_bytes(prefix).map(|s| (s, remainder))
-        }
+        Self::from_bytes(prefix).map(|s| (s, remainder))
     }
 
     /// Converts a mutable slice of bytes to a reference to `Self`.
@@ -110,16 +103,9 @@ fn from_bytes_mut_prefix(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
     where
         Self: AsBytes + Sized,
     {
-        if bytes.len() < size_of::<Self>() {
-            None
-        } else {
-            // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
-            // panic.
-            // TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80.
-            let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
+        let (prefix, remainder) = bytes.split_at_mut_checked(size_of::<Self>())?;
 
-            Self::from_bytes_mut(prefix).map(|s| (s, remainder))
-        }
+        Self::from_bytes_mut(prefix).map(|s| (s, remainder))
     }
 
     /// Creates an owned instance of `Self` by copying `bytes`.
@@ -149,16 +135,9 @@ fn from_bytes_copy_prefix(bytes: &[u8]) -> Option<(Self, &[u8])>
     where
         Self: Sized,
     {
-        if bytes.len() < size_of::<Self>() {
-            None
-        } else {
-            // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
-            // panic.
-            // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
-            let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+        let (prefix, remainder) = bytes.split_at_checked(size_of::<Self>())?;
 
-            Self::from_bytes_copy(prefix).map(|s| (s, remainder))
-        }
+        Self::from_bytes_copy(prefix).map(|s| (s, remainder))
     }
 }
 
-- 
2.43.0


             reply	other threads:[~2026-03-10  9:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10  9:57 Albab Hasan [this message]
2026-03-10 10:03 ` [PATCH] rust: transmute: use split_at_checked() and split_at_mut_checked() Miguel Ojeda
     [not found]   ` <CAM9eepV-3Oh8yY4JaLtiU9bbxJfA+Kf9m-yZdOuVfp-pHQ3+=Q@mail.gmail.com>
     [not found]     ` <CANiq72kqq_WKFPLkcykv8WbhVWmDt=g0dYnjZUYEehxMrmkOQw@mail.gmail.com>
2026-03-11  6:44       ` Albab Hasan
2026-03-10 12:57 ` 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=20260310095710.1166-1-albabhasan276@gmail.com \
    --to=albabhasan276@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --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