2022-01-10 20:56:10

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

The literals "big-endian" and "little-endian" may be potentially
occurred in other places. Dropping space allows compiler to
"compress" them by using only a single copy.

Cc: Sakari Ailus <[email protected]>
Signed-off-by: Andy Shevchenko <[email protected]>
---

Depends on the
https://lore.kernel.org/lkml/[email protected]/T/#u
but not to be critical or fix-like.

lib/vsprintf.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index b02f01366acb..5818856d5626 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1780,7 +1780,8 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
*p++ = isascii(c) && isprint(c) ? c : '.';
}

- strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
+ *p++ = ' ';
+ strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
p += strlen(p);

*p++ = ' ';
--
2.34.1



2022-01-10 22:13:37

by Sakari Ailus

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

On Mon, Jan 10, 2022 at 10:55:58PM +0200, Andy Shevchenko wrote:
> The literals "big-endian" and "little-endian" may be potentially
> occurred in other places. Dropping space allows compiler to
> "compress" them by using only a single copy.
>
> Cc: Sakari Ailus <[email protected]>
> Signed-off-by: Andy Shevchenko <[email protected]>

Acked-by: Sakari Ailus <[email protected]>

--
Sakari Ailus

2022-01-11 10:26:27

by Rasmus Villemoes

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

On 10/01/2022 21.55, Andy Shevchenko wrote:
> The literals "big-endian" and "little-endian" may be potentially
> occurred in other places. Dropping space allows compiler to
> "compress" them by using only a single copy.

Nit: it's not the compiler which does that, but the linker.

> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index b02f01366acb..5818856d5626 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -1780,7 +1780,8 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
> *p++ = isascii(c) && isprint(c) ? c : '.';
> }
>
> - strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
> + *p++ = ' ';
> + strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
> p += strlen(p);

Hm, ok, those two strings do occur a lot with of_property_read_bool()
and friends. But if you're micro-optimizing anyway, why not drop the
strlen() and say p = stpcpy(...) instead?
Rasmus

2022-01-11 11:29:39

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

On Tue, Jan 11, 2022 at 11:26:21AM +0100, Rasmus Villemoes wrote:
> On 10/01/2022 21.55, Andy Shevchenko wrote:
> > The literals "big-endian" and "little-endian" may be potentially
> > occurred in other places. Dropping space allows compiler to
> > "compress" them by using only a single copy.
>
> Nit: it's not the compiler which does that, but the linker.

Ah, I stand corrected, thanks!

> > - strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
> > + *p++ = ' ';
> > + strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
> > p += strlen(p);
>
> Hm, ok, those two strings do occur a lot with of_property_read_bool()
> and friends. But if you're micro-optimizing anyway, why not drop the
> strlen() and say p = stpcpy(...) instead?

Why not? I'll do it for v2.

Any thoughts / comments on the
https://lore.kernel.org/lkml/[email protected]/T/#u?
I'm asking since dependency and I would like to know if we still want that
fix or not.

--
With Best Regards,
Andy Shevchenko



2022-01-11 15:10:29

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

On Tue 2022-01-11 13:28:21, Andy Shevchenko wrote:
> On Tue, Jan 11, 2022 at 11:26:21AM +0100, Rasmus Villemoes wrote:
> > On 10/01/2022 21.55, Andy Shevchenko wrote:
> > > The literals "big-endian" and "little-endian" may be potentially
> > > occurred in other places. Dropping space allows compiler to
> > > "compress" them by using only a single copy.
> >
> > Nit: it's not the compiler which does that, but the linker.
>
> Ah, I stand corrected, thanks!
>
> > > - strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
> > > + *p++ = ' ';
> > > + strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
> > > p += strlen(p);
> >
> > Hm, ok, those two strings do occur a lot with of_property_read_bool()
> > and friends. But if you're micro-optimizing anyway, why not drop the
> > strlen() and say p = stpcpy(...) instead?
>
> Why not? I'll do it for v2.

It is safe here. I just hope that it will not trigger reports from
some tools looking for potential security issues ;-)

> Any thoughts / comments on the
> https://lore.kernel.org/lkml/[email protected]/T/#u?
> I'm asking since dependency and I would like to know if we still want that
> fix or not.

Just commented there. It looks fine to me.

Best Regards,
Petr

2022-01-11 16:14:48

by Rasmus Villemoes

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] vsprintf: Move space out of string literals in fourcc_string()

On 11/01/2022 16.10, Petr Mladek wrote:
> On Tue 2022-01-11 13:28:21, Andy Shevchenko wrote:

>>>> - strcpy(p, orig & BIT(31) ? " big-endian" : " little-endian");
>>>> + *p++ = ' ';
>>>> + strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
>>>> p += strlen(p);
>>>
>>> Hm, ok, those two strings do occur a lot with of_property_read_bool()
>>> and friends. But if you're micro-optimizing anyway, why not drop the
>>> strlen() and say p = stpcpy(...) instead?
>>
>> Why not? I'll do it for v2.
>
> It is safe here. I just hope that it will not trigger reports from
> some tools looking for potential security issues ;-)
>

strcpy() and stpcpy() have exactly the same preconditions and are safe
in exactly the same circumstances, they only differ in their return
value, so I don't see how any tool would warn about a use of (the
only-recently-standardized) stpcpy if it didn't already warn about strcpy.

Rasmus