All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [v2] led-class-flash: fix -Wrestrict warning
@ 2021-09-27 13:15 Arnd Bergmann
  2021-09-27 14:09 ` Pavel Machek
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2021-09-27 13:15 UTC (permalink / raw)
  To: Pavel Machek; +Cc: Arnd Bergmann, linux-leds, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

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.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/leds/led-class-flash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/led-class-flash.c b/drivers/leds/led-class-flash.c
index 185e17055317..6fe9d700dfef 100644
--- a/drivers/leds/led-class-flash.c
+++ b/drivers/leds/led-class-flash.c
@@ -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);
 
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] [v2] led-class-flash: fix -Wrestrict warning
  2021-09-27 13:15 [PATCH] [v2] led-class-flash: fix -Wrestrict warning Arnd Bergmann
@ 2021-09-27 14:09 ` Pavel Machek
  0 siblings, 0 replies; 2+ messages in thread
From: Pavel Machek @ 2021-09-27 14:09 UTC (permalink / raw)
  To: Arnd Bergmann; +Cc: Arnd Bergmann, linux-leds, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 818 bytes --]

Hi!

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

Thanks, applied.
								Pavel
-- 
http://www.livejournal.com/~pavelmachek

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-27 14:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27 13:15 [PATCH] [v2] led-class-flash: fix -Wrestrict warning Arnd Bergmann
2021-09-27 14:09 ` Pavel Machek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.