public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Tamir Duberstein" <tamird@kernel.org>, "Gary Guo" <gary@garyguo.net>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: disallow use of `CStr::as_ptr`
Date: Wed, 21 Jan 2026 18:19:34 +0000	[thread overview]
Message-ID: <DFUH3PDOQCZ2.2QQSIB9IQIFO1@garyguo.net> (raw)
In-Reply-To: <CAJ-ks9=J564pjPbMBaq5MOHy2W5MhyghR-0B1B4XwWQVVnEOSQ@mail.gmail.com>

On Wed Jan 21, 2026 at 5:55 PM GMT, Tamir Duberstein wrote:
> On Wed, Jan 21, 2026 at 11:58 AM Gary Guo <gary@kernel.org> wrote:
>>
>> From: Gary Guo <gary@garyguo.net>
>>
>> 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.
>>
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>> ---
>>  .clippy.toml       | 5 +++++
>>  rust/kernel/str.rs | 2 ++
>>  2 files changed, 7 insertions(+)
>>
>> diff --git a/.clippy.toml b/.clippy.toml
>> index 137f41d203de..fd934bc04242 100644
>> --- a/.clippy.toml
>> +++ b/.clippy.toml
>> @@ -9,3 +9,8 @@ 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."
>
> In the disallowed-macros section above we seem to use sentence
> fragments (no capitalization, no trailing period).

Ok.

>
>> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> index fa87779d2253..08b8e2ebc8ad 100644
>> --- a/rust/kernel/str.rs
>> +++ b/rust/kernel/str.rs
>> @@ -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()
>>  }
>> @@ -334,6 +335,7 @@ unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut Self {
>>      }
>>
>>      #[inline]
>> +    #[expect(clippy::disallowed_methods, reason = "internal implementation")]
>
> Can this be inside the function, on the `as_ptr` call itself?

You cannot apply attributes on expressions (yet). If this is to be moved into
the function, there needs an additional block, which I think is just
unnecessary.

Best,
Gary

>
>>      fn as_char_ptr(&self) -> *const c_char {
>>          self.as_ptr().cast()
>>      }
>>
>> --
>> 2.51.2
>>
>
> Acked-by: Tamir Duberstein <tamird@kernel.org>


  reply	other threads:[~2026-01-21 18:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-21 16:58 [PATCH] rust: disallow use of `CStr::as_ptr` Gary Guo
2026-01-21 17:04 ` Miguel Ojeda
2026-01-21 17:33   ` Gary Guo
2026-01-21 18:11     ` Miguel Ojeda
2026-01-21 17:55 ` Tamir Duberstein
2026-01-21 18:19   ` Gary Guo [this message]
2026-01-21 19:45     ` Tamir Duberstein
2026-01-21 20:01       ` Gary Guo
2026-01-21 20:01         ` Tamir Duberstein
2026-01-21 20:47           ` Miguel Ojeda
2026-01-22  8:28 ` Alice Ryhl
2026-01-22 10:57 ` kernel test robot

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=DFUH3PDOQCZ2.2QQSIB9IQIFO1@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    /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