Skip to contentSkip to conversation

vsprintf: Move space out of string literals in fourcc_string()

PATCH In review Andy Shevchenko 4 participants 6 messages Jan 10, 2022 – Jan 11, 2022

Subsystems (2)
lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 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.

---

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(-)
lib/vsprintf.c +2 -1
index b02f01366acb..5818856d5626 100644
@@ -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++ = ' ';
Signature · 1 line
2.34.1
#2 lore raw
In reply to Andy Shevchenko
Quoted 3 lines · Andy Shevchenko

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.

lib/vsprintf.c char *fourcc_string(char *buf, char *end, const u32 *fourcc, · line 1785

Quoted 12 lines
lib/vsprintf.c +2 -1
index b02f01366acb..5818856d5626 100644
@@ -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

author reviewer
#3 lore raw
Quoted 6 lines · Rasmus Villemoes
Quoted 3 lines · Andy Shevchenko

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!

Quoted 8 lines

lib/vsprintf.c char *fourcc_string(char *buf, char *end, const u32 *fourcc, · line 1785

Quoted 4 lines
lib/vsprintf.c +2 -1
-	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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw
Quoted 19 lines · Andy Shevchenko
Quoted 6 lines · Rasmus Villemoes
Quoted 3 lines · Andy Shevchenko

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!

Quoted 8 lines

lib/vsprintf.c char *fourcc_string(char *buf, char *end, const u32 *fourcc, · line 1785

Quoted 4 lines
lib/vsprintf.c +2 -1
-	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?

Quoted 1 line

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 ;-)

Quoted 4 lines

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

#5 lore raw
Quoted 11 lines · Petr Mladek
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines

lib/vsprintf.c char *fourcc_string(char *buf, char *end, const u32 *fourcc, · line 1785

Quoted 4 lines
lib/vsprintf.c +2 -1
-	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?

Quoted 5 lines
Quoted 1 line

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