rust: uaccess: add UserSliceReader::strcpy_into_buf
authorAlice Ryhl <aliceryhl@google.com>
Mon, 16 Jun 2025 12:41:48 +0000 (12:41 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 13 Jul 2025 23:34:12 +0000 (01:34 +0200)
commit17bbbefbf6715a543ff4713e26f7b8e6b7a876d6
treec0937e87db7f6bb5dcdc1df8a0c5a51161c66f63
parent8da881d39c1b7fd4a211587ba40f1c936909a11a
rust: uaccess: add UserSliceReader::strcpy_into_buf

This patch adds a more convenient method for reading C strings from
userspace. Logic is added to NUL-terminate the buffer when necessary so
that a &CStr can be returned.

Note that we treat attempts to read past `self.length` as a fault, so
this returns EFAULT if that limit is exceeded before `buf.len()` is
reached.

Reviewed-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250616-strncpy-from-user-v5-2-2d3fb0e1f5af@google.com
[ Use `from_mut` to clean `clippy::ref_as_ptr` lint. Reworded
  title. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/uaccess.rs