Re: [PATCH v4 12/20] vsprintf: add new `%pA` format specifier

From: Rasmus Villemoes
Date: Mon Feb 14 2022 - 06:19:20 EST


On 14/02/2022 11.18, Andy Shevchenko wrote:
> On Sat, Feb 12, 2022 at 02:03:38PM +0100, Miguel Ojeda wrote:
>
>> From: Gary Guo <gary@xxxxxxxxxxx>
>
> Not sure I understand this...
>
>> This patch adds a format specifier `%pA` to `vsprintf` which formats
>> a pointer as `core::fmt::Arguments`. Doing so allows us to directly
>> format to the internal buffer of `printf`, so we do not have to use
>> a temporary buffer on the stack to pre-assemble the message on
>> the Rust side.
>>
>> This specifier is intended only to be used from Rust and not for C, so
>> `checkpatch.pl` is intentionally unchanged to catch any misuse.
>>
>> Co-developed-by: Alex Gaynor <alex.gaynor@xxxxxxxxx>
>> Signed-off-by: Alex Gaynor <alex.gaynor@xxxxxxxxx>
>> Co-developed-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxxx>
>> Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxxx>
>
>> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
>
> ...together with this in the current SoB chain.
>
>> Co-developed-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
>> Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
>
> I'm wondering if you considered to use %pV.
>

I think the point is for vsnprintf() to call (back) into Rust code.

That said, I don't like the !CONFIG_RUST version to return NULL, that
will surely crash moments later.

So I prefer something like

[rust.h]
// no CONFIG_RUST conditional
+char *rust_fmt_argument(char* buf, char* end, void *ptr);

[vsprintf.c]
+ case 'A':
+ if (IS_ENABLED(CONFIG_RUST))
+ return rust_fmt_argument(buf, end, ptr);
+ else
+ return string_nocheck(buf, end, "[%pA in non-Rust
code?!]", default_str_spec);


Rasmus