Skip to contentSkip to conversation

PM: Replace snprintf() with scnprintf() in show_trace_dev_match()

PATCH Applied Kaushlendra Kumar 2 participants 2 messages Sep 22, 2025 – Oct 13, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: Replace snprintf() with scnprintf() in show_trace_dev_match()

Replace snprintf() with scnprintf() in show_trace_dev_match() to simplify buffer length handling. The scnprintf() function returns the number of characters actually written (excluding the null terminator), which eliminates the need for manual length checking and clamping.

This change removes the redundant size check since scnprintf() guarantees that the return value will never exceed the buffer size, making the code cleaner and less error-prone.

---
 drivers/base/power/trace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/power/trace.c +1 -3
index cd6e559648b2..d8da7195bb00 100644
@@ -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;
Signature · 1 line
2.34.1
maintainer Applied
#1 lore raw
Quoted 22 lines · Kaushlendra Kumar

Replace snprintf() with scnprintf() in show_trace_dev_match() to simplify buffer length handling. The scnprintf() function returns the number of characters actually written (excluding the null terminator), which eliminates the need for manual length checking and clamping.

This change removes the redundant size check since scnprintf() guarantees that the return value will never exceed the buffer size, making the code cleaner and less error-prone.

---
 drivers/base/power/trace.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/power/trace.c +0 -0
index cd6e559648b2..d8da7195bb00 100644
@@ -238,10 +238,8 @@ int show_trace_dev_match(char *buf, size_t size)
                unsigned int hash = hash_string(DEVSEED, dev_name(dev),
                                                DEVHASH);

drivers/base/power/trace.c int show_trace_dev_match(char *buf, size_t size)

Quoted 10 lines
drivers/base/power/trace.c +1 -4
                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;
--

Applied as 6.19 material, thanks!