Skip to contentSkip to conversation

Input: analog - fix invalid snprintf() call

PATCH Superseded Arnd Bergmann 3 participants 3 messages Mar 23, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: analog - fix invalid snprintf() call

From: Arnd Bergmann <[email protected]>

overlapping input and output arguments to snprintf() are undefined behavior in C99:

drivers/input/joystick/analog.c: In function 'analog_name':
drivers/input/joystick/analog.c:428:3: error: 'snprintf' argument 4 overlaps destination object 'analog' [-Werror=restrict]

  428 |   snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  429 |     analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/input/joystick/analog.c:420:40: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  420 | static void analog_name(struct analog *analog)
      |                         ~~~~~~~~~~~~~~~^~~~~~

Change this function to just use the offset it already knows.

---
 drivers/input/joystick/analog.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/input/joystick/analog.c +5 -3
index f798922a4598..8c9fed3f13e2 100644
@@ -419,14 +419,16 @@ static void analog_calibrate_timer(struct analog_port *port)
 
 static void analog_name(struct analog *analog)
 {
-	snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
+	int len;
+
+	len = snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
 		 hweight8(analog->mask & ANALOG_AXES_STD),
 		 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
 		 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
 
 	if (analog->mask & ANALOG_HATS_ALL)
-		snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
-			 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
+		len += snprintf(analog->name + len, sizeof(analog->name) - len, "%d-hat",
+			 hweight16(analog->mask & ANALOG_HATS_ALL));
 
 	if (analog->mask & ANALOG_HAT_FCS)
 		strlcat(analog->name, " FCS", sizeof(analog->name));
Signature · 1 line
2.29.2
#1 lore raw
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

overlapping input and output arguments to snprintf() are undefined behavior in C99:

Good luck:
https://lore.kernel.org/lkml/[email protected]/

At least 5 years ago the consensus from old-timers was that "the kernel's snprintf supports this use case, just keep it working that way".

Quoted 11 lines
drivers/input/joystick/analog.c +2 -1
index f798922a4598..8c9fed3f13e2 100644
@@ -419,14 +419,16 @@ static void analog_calibrate_timer(struct analog_port *port)
 
 static void analog_name(struct analog *analog)
 {
-	snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
+	int len;
+

drivers/input/joystick/analog.c static void analog_calibrate_timer(struct analog_port *port) · line 431

Quoted 10 lines
drivers/input/joystick/analog.c +3 -2
+	len = snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
 		 hweight8(analog->mask & ANALOG_AXES_STD),
 		 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
 		 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
 
 	if (analog->mask & ANALOG_HATS_ALL)
-		snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
-			 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
+		len += snprintf(analog->name + len, sizeof(analog->name) - len, "%d-hat",
+			 hweight16(analog->mask & ANALOG_HATS_ALL));

Use scnprintf, this is too fragile and hard to verify. If the first snprintf overflows, the second passes a huge size_t to snprintf which will WARN.

Rasmus

maintainer
#2 lore raw
Quoted 29 lines · Rasmus Villemoes
Quoted 5 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

overlapping input and output arguments to snprintf() are undefined behavior in C99:

Good luck:
https://lore.kernel.org/lkml/[email protected]/

At least 5 years ago the consensus from old-timers was that "the kernel's snprintf supports this use case, just keep it working that way".

Quoted 16 lines
drivers/input/joystick/analog.c +3 -1
index f798922a4598..8c9fed3f13e2 100644
@@ -419,14 +419,16 @@ static void analog_calibrate_timer(struct analog_port *port)
 
 static void analog_name(struct analog *analog)
 {
-	snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
+	int len;
+
+	len = snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
 		 hweight8(analog->mask & ANALOG_AXES_STD),
 		 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
 		 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
 
Quoted 9 lines

drivers/input/joystick/analog.c static void analog_calibrate_timer(struct analog_port *port) · line 431

Quoted 5 lines
drivers/input/joystick/analog.c +2 -2
 	if (analog->mask & ANALOG_HATS_ALL)
-		snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
-			 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
+		len += snprintf(analog->name + len, sizeof(analog->name) - len, "%d-hat",
+			 hweight16(analog->mask & ANALOG_HATS_ALL));

Use scnprintf, this is too fragile and hard to verify. If the first snprintf overflows, the second passes a huge size_t to snprintf which will WARN.

Also the format needs to be " %d-hat" (note the leading space).

Thanks.

Signature · 1 line
Dmitry