rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Timur Tabi" <ttabi@nvidia.com>,
	"ojeda@kernel.org" <ojeda@kernel.org>,
	"aliceryhl@google.com" <aliceryhl@google.com>,
	"dakr@kernel.org" <dakr@kernel.org>,
	"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v3] rust: introduce sfile macro for succinct code tracing
Date: Sat, 14 Jun 2025 20:01:13 +0200	[thread overview]
Message-ID: <DAMGB9H9NV06.11M9CU65BVLXG@kernel.org> (raw)
In-Reply-To: <96ee11ca8f198892b35fc07c72ebdbb06095544e.camel@nvidia.com>

On Sat Jun 14, 2025 at 12:46 AM CEST, Timur Tabi wrote:
> On Tue, 2025-06-10 at 10:45 +0200, Benno Lossin wrote:
>> > +            Some(slash) => slash + 1,
>> > +            None => 0,
>> > +        };
>> > +        let end = match rfind_const(FILE, '.') {
>> > +            Some(dot) => dot,
>> > +            None => FILE.len(),
>> > +        };
>> > +
>> > +        // The following code the equivalent of &FILE[start..start+len],
>> > +        // except that it allows for const contexts.  For example,
>> > +        //   const SFILE: &'static str = sfile!();
>> > +
>> > +        let base_ptr: *const u8 = FILE.as_ptr();
>> > +
>> > +        // SAFETY: We know that `start` is <= the length of FILE, because FILE
>> > +        // never ends in a slash.
>> 
>> I don't think that we should rely on that fact for unsafe code.
>> 
>> There is another argument for `start <= length`: when `rfind_const`
>> returns `Some(res)`, then `res < length`.
>
> The problem is the +1:
>
>     let start = match ::kernel::str::rfind_const(FILE, '/') {
>         Some(slash) => slash + 1,
>         None => 0,
>     };
>
> `start` is the first character *after* the slash.  If FILE ever did end in a slash, then `start`
> would point past the end of the string.  That didn't seem to cause a problem when I tested it, but
> technically my code just assumes it's true.
>
> I could add another assertion:
>
>             assert!(start < FILE.len());
>
> If I set FILE to "HELLO\", this causes a compiler error:
>
> 178 |         pr_info!("{}:{}\n", sfile!(), line!());
>     |                             ^^^^^^^^ the evaluated program panicked at 'assertion failed:
> start < FILE.len()', drivers/gpu/nova-core/gpu.rs:178:29
>
> or even:
>
>             assert!(start < end);
>
> This code is only ever supposed on be used on ASCII filenames, which can never end in a slash.
>
> I could do something like this:
>
>             let start = match ::kernel::str::rfind_const(FILE, '/') {
>                 Some(slash) if slash == FILE.len() => slash,
>                 Some(slash) => slash + 1,
>                 None => 0,
>             };
>
> But I don't think this is useful.  

Let's use an assert, since it shouldn't happen in practice. We need to
assert it though for the unsafe code to be correct.

---
Cheers,
Benno

  reply	other threads:[~2025-06-14 18:01 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-09 22:36 [PATCH v3] rust: introduce sfile macro for succinct code tracing Timur Tabi
2025-06-10  2:33 ` John Hubbard
2025-06-10  3:40   ` Timur Tabi
2025-06-10  8:31     ` Benno Lossin
2025-06-10  8:45 ` Benno Lossin
2025-06-13 17:03   ` Timur Tabi
2025-06-13 17:57     ` Miguel Ojeda
2025-06-13 19:14       ` Timur Tabi
2025-06-13 20:08         ` Timur Tabi
2025-06-13 19:32   ` Timur Tabi
2025-06-13 20:06     ` Timur Tabi
2025-06-14 17:08       ` Benno Lossin
2025-06-16 15:37         ` Timur Tabi
2025-06-16 18:18           ` Miguel Ojeda
2025-06-13 22:46   ` Timur Tabi
2025-06-14 18:01     ` Benno Lossin [this message]
2025-06-10  8:47 ` Miguel Ojeda
2025-06-10 19:18   ` Timur Tabi
2025-06-10 19:29     ` Miguel Ojeda

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=DAMGB9H9NV06.11M9CU65BVLXG@kernel.org \
    --to=lossin@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=ttabi@nvidia.com \
    /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;
as well as URLs for NNTP newsgroup(s).