Linux Power Management development
 help / color / mirror / Atom feed
* [RESEND PATCH] drivers: base: power: Optimize array out-of-bounds access logic
@ 2024-12-02  3:23 xueqin Luo
  2024-12-02  6:21 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: xueqin Luo @ 2024-12-02  3:23 UTC (permalink / raw)
  To: rafael, pavel, len.brown, gregkh; +Cc: linux-pm, linux-kernel, xueqin Luo

The code previously used snprintf to format a string into a buffer and
manually checked for potential buffer overflows by comparing the returned
length with the buffer size. This approach introduced unnecessary
complexity and was prone to subtle errors.

Replaced snprintf with scnprintf, which directly returns the actual number
of characters written to the buffer (excluding the null terminator). This
change eliminates the need for manual overflow checks and simplifies the
buffer offset and size adjustment logic.

Signed-off-by: xueqin Luo <luoxueqin@kylinos.cn>
---
 drivers/base/power/trace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c
index cd6e559648b2..d8da7195bb00 100644
--- a/drivers/base/power/trace.c
+++ b/drivers/base/power/trace.c
@@ -238,10 +238,8 @@ int show_trace_dev_match(char *buf, size_t size)
 		unsigned int hash = hash_string(DEVSEED, dev_name(dev),
 						DEVHASH);
 		if (hash == value) {
-			int len = snprintf(buf, size, "%s\n",
+			int len = scnprintf(buf, size, "%s\n",
 					    dev_driver_string(dev));
-			if (len > size)
-				len = size;
 			buf += len;
 			ret += len;
 			size -= len;
-- 
2.34.1


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

end of thread, other threads:[~2024-12-02  6:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-02  3:23 [RESEND PATCH] drivers: base: power: Optimize array out-of-bounds access logic xueqin Luo
2024-12-02  6:21 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox