]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: disallow use of `CStr::as_ptr` and `CStr::from_ptr`
authorGary Guo <gary@garyguo.net>
Tue, 3 Feb 2026 13:06:27 +0000 (13:06 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Tue, 10 Mar 2026 23:33:40 +0000 (00:33 +0100)
As kernel always use unsigned char and not the platform ABI's default, an
user should always use `as_char_ptr` provided via `CStrExt` instead.
Therefore configure `disallow-methods` feature of clippy to catch incorrect
usage.

Similarly, the dual `from_ptr` is also disallowed.

[ As an example, without the previous commit, we would get a warning like:

      warning: use of a disallowed method `core::ffi::CStr::as_ptr`
         --> rust/kernel/task.rs:422:54
          |
      422 |         unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
          |                                                      ^^^^^^ help: kernel's `char` is always unsigned, use `as_char_ptr` instead: `kernel::prelude::CStrExt::as_char_ptr`
          |
          = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.94.0/index.html#disallowed_methods
          = note: `-W clippy::disallowed-methods` implied by `-W clippy::all`
          = help: to override `-W clippy::all` add `#[allow(clippy::disallowed_methods)]`

    - Miguel ]

Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Tamir Duberstein <tamird@kernel.org>
Link: https://patch.msgid.link/20260203130745.868762-2-gary@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
.clippy.toml
rust/kernel/str.rs

index 137f41d203de377aacbbe237086f05d6a7ac3bab..a51de9a46380316a4dbad04da005e6cf8c60d0b9 100644 (file)
@@ -9,3 +9,13 @@ disallowed-macros = [
     # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303.
     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool", allow-invalid = true },
 ]
+
+[[disallowed-methods]]
+path = "core::ffi::CStr::as_ptr"
+replacement = "kernel::prelude::CStrExt::as_char_ptr"
+reason = "kernel's `char` is always unsigned, use `as_char_ptr` instead"
+
+[[disallowed-methods]]
+path = "core::ffi::CStr::from_ptr"
+replacement = "kernel::prelude::CStrExt::from_char_ptr"
+reason = "kernel's `char` is always unsigned, use `from_char_ptr` instead"
index fa87779d22539fa4bb20cd923dc411a3e976fa30..97bf9427af59b7559ddca565d80afa32e3d3f422 100644 (file)
@@ -189,6 +189,7 @@ macro_rules! b_str {
 //
 // - error[E0379]: functions in trait impls cannot be declared const
 #[inline]
+#[expect(clippy::disallowed_methods, reason = "internal implementation")]
 pub const fn as_char_ptr_in_const_context(c_str: &CStr) -> *const c_char {
     c_str.as_ptr().cast()
 }
@@ -319,6 +320,7 @@ unsafe fn to_bytes_mut(s: &mut CStr) -> &mut [u8] {
 
 impl CStrExt for CStr {
     #[inline]
+    #[expect(clippy::disallowed_methods, reason = "internal implementation")]
     unsafe fn from_char_ptr<'a>(ptr: *const c_char) -> &'a Self {
         // SAFETY: The safety preconditions are the same as for `CStr::from_ptr`.
         unsafe { CStr::from_ptr(ptr.cast()) }
@@ -334,6 +336,7 @@ impl CStrExt for CStr {
     }
 
     #[inline]
+    #[expect(clippy::disallowed_methods, reason = "internal implementation")]
     fn as_char_ptr(&self) -> *const c_char {
         self.as_ptr().cast()
     }