Skip to contentSkip to conversation

firmware/memmap: use scnprintf() in show funcs

PATCH In review Pranav Tyagi 2 participants 3 messages Jun 13, 2025 – Jul 1, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware/memmap: use scnprintf() in show funcs

Replace all snprintf() instances with scnprintf(). snprintf() returns the number of bytes that would have been written had there been enough space. For sysfs attributes, snprintf() should not be used for the show() method. Instead use scnprintf() which returns the number of bytes actually written.

---
 drivers/firmware/memmap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/memmap.c +3 -3
index 55b9cfad8a04..fef18f598ff8 100644
@@ -369,19 +369,19 @@ int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
 
 static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
+	return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
 		(unsigned long long)entry->start);
 }
 
 static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
+	return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
 		(unsigned long long)entry->end);
 }
 
 static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
+	return scnprintf(buf, PAGE_SIZE, "%s\n", entry->type);
 }
 
 static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
Signature · 1 line
2.49.0
#1 lore raw
Quoted 5 lines · Pranav Tyagi

Replace all snprintf() instances with scnprintf(). snprintf() returns the number of bytes that would have been written had there been enough space. For sysfs attributes, snprintf() should not be used for the show() method. Instead use scnprintf() which returns the number of bytes actually written.

for sysfs attributes, sysfs_emit() should be used instead. Why not do that here?

But what is wrong with the current code? Is it not working properly?

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · Greg KH
Quoted 5 lines · Pranav Tyagi

Replace all snprintf() instances with scnprintf(). snprintf() returns the number of bytes that would have been written had there been enough space. For sysfs attributes, snprintf() should not be used for the show() method. Instead use scnprintf() which returns the number of bytes actually written.

Quoted 8 lines

for sysfs attributes, sysfs_emit() should be used instead. Why not do that here?

But what is wrong with the current code? Is it not working properly?

thanks,

greg k-h'

I had sent this patch earlier along with a few others of similar nature. The code in question was working correctly. However, the intent was to perform cleanups based on documentation guidance.

Specifically, for sysfs attributes, the documentation discourages using snprintf() in the show() method since it returns the number of bytes that would have been written, not the number actually written. This can potentially lead to buffer overflows.

You had previously pointed out that either sysfs_emit() should be used or the code should be left unchanged if it is working correctly. To avoid unnecessary churn, I decided to drop those ( including this one ) patches entirely.

Regards
Pranav Tyagi