[PATCH v6 2/2] rust: uaccess: refactor to use `overflow_assert!`

From: Antonio Hickey

Date: Wed Jul 01 2026 - 22:20:13 EST


Using the `overflow_assert!` macro here adds documentation to
the intent of the assertion, and avoids local `#ifdefs`s by
encapsulating the conditional behavior to the macro itself.

Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1159
Co-developed-by: Daniel Cote <danielstonecote@xxxxxxxxx>
Signed-off-by: Daniel Cote <danielstonecote@xxxxxxxxx>
Signed-off-by: Antonio Hickey <contact@xxxxxxxxxxxxxxxxx>
Reviewed-by: Tamir Duberstein <tamird@xxxxxxxxx>
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
rust/kernel/uaccess.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 5f6c4d7a1a51..f04c913f23b6 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -11,6 +11,7 @@
error::Result,
ffi::{c_char, c_void},
fs::file,
+ overflow_assert,
prelude::*,
ptr::KnownSize,
transmute::{AsBytes, FromBytes},
@@ -655,8 +656,7 @@ fn raw_strncpy_from_user(dst: &mut [MaybeUninit<u8>], src: UserPtr) -> Result<us
return Err(Error::from_errno(res as i32));
}

- #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
- assert!(res <= len);
+ overflow_assert!(res <= len, "strncpy_from_user wrote past dst buffer length");

// GUARANTEES: `strncpy_from_user` was successful, so `dst` has contents in accordance with the
// guarantees of this function.
--
2.54.0