Skip to contentSkip to conversation

led-class-flash: fix -Wrestrict warning

PATCH Applied Arnd Bergmann 2 participants 2 messages Sep 27, 2021 LED

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 led-class-flash: fix -Wrestrict warning

From: Arnd Bergmann <[email protected]>

gcc-11 warns when building with W=1:

drivers/leds/led-class-flash.c: In function 'flash_fault_show':
drivers/leds/led-class-flash.c:210:16: error: 'sprintf' argument 3 overlaps destination object 'buf' [-Werror=restrict]

  210 |         return sprintf(buf, "%s\n", buf);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~

drivers/leds/led-class-flash.c:187:54: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
187 | struct device_attribute *attr, char *buf)
| ~~~~~~^~~

There is no need for the sprintf() here when a strcat() does the same thing without invoking undefined behavior.

---
 drivers/leds/led-class-flash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/led-class-flash.c +1 -1
index 185e17055317..6fe9d700dfef 100644
@@ -207,7 +207,7 @@ static ssize_t flash_fault_show(struct device *dev,
 		mask <<= 1;
 	}
 
-	return sprintf(buf, "%s\n", buf);
+	return strlen(strcat(buf, "\n"));
 }
 static DEVICE_ATTR_RO(flash_fault);
 
Signature · 1 line
2.29.2
Applied
#1 lore raw

Hi!

Quoted 4 lines

gcc-11 warns when building with W=1:

drivers/leds/led-class-flash.c: In function 'flash_fault_show':
drivers/leds/led-class-flash.c:210:16: error: 'sprintf' argument 3 overlaps destination object 'buf' [-Werror=restrict]

Quoted 8 lines
  210 |         return sprintf(buf, "%s\n", buf);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~

drivers/leds/led-class-flash.c:187:54: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
187 | struct device_attribute *attr, char *buf)
| ~~~~~~^~~

There is no need for the sprintf() here when a strcat() does the same thing without invoking undefined behavior.

Thanks, applied.
Pavel

Signature · 1 line
http://www.livejournal.com/~pavelmachek