Re: [PATCH] uaccess: rust: add strncpy_from_user

From: Danilo Krummrich
Date: Thu Apr 24 2025 - 12:34:38 EST


On Thu, Apr 24, 2025 at 03:17:48PM +0000, Alice Ryhl wrote:
>
> +/// Reads a nul-terminated string into `buf` and returns the length.
> +///
> +/// Fails with [`EFAULT`] if the read happens on a bad address. If the end of `buf` is reached,
> +/// then the buffer will not be nul-terminated.
> +#[inline]
> +pub fn strncpy_from_user(ptr: UserPtr, buf: &mut [u8]) -> Result<usize> {

Should probably be named strcpy_from_user() instead.

> + // CAST: Slice lengths are guaranteed to be `<= isize::MAX`.
> + let len = buf.len() as isize;
> +
> + // SAFETY: `buf` is valid for writing `buf.len()` bytes.
> + let res = unsafe {
> + bindings::strncpy_from_user(
> + buf.as_mut_ptr(),
> + ptr as *const u8,

kernel::ffi::c_char should always match u8, but should we use the FFI type
regardless?