Skip to contentSkip to conversation

driver core: Use scnprintf() instead of snprintf() in device show functions

PATCH In review Denis Efremov 2 participants 2 messages Jun 20, 2020 – Jun 21, 2020 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: Use scnprintf() instead of snprintf() in device show functions

From Documentation/filesystems/sysfs.txt:
- show() must not use snprintf() when formatting the value to be returned to user space. If you can guarantee that an overflow will never happen you can use sprintf() otherwise you must use scnprintf().

The reason is that snprintf() returns the length the resulting string would be, assuming the data will fit into the destination array. scnprintf() returns the length of the string actually created in buf.

device_show_{ulong,int,bool}() functions are reference implementation and should respect the kernel documentation.

---

Actually, I don't know why snprintf() is used here initially. It looks safe to use sprintf().

 drivers/base/core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/base/core.c +3 -3
index 67d39a90b45c..2815deb511fc 100644
@@ -1472,7 +1472,7 @@ ssize_t device_show_ulong(struct device *dev,
 			  char *buf)
 {
 	struct dev_ext_attribute *ea = to_ext_attr(attr);
-	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
+	return scnprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
 }
 EXPORT_SYMBOL_GPL(device_show_ulong);
 
@@ -1502,7 +1502,7 @@ ssize_t device_show_int(struct device *dev,
 {
 	struct dev_ext_attribute *ea = to_ext_attr(attr);
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
+	return scnprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
 }
 EXPORT_SYMBOL_GPL(device_show_int);
 
@@ -1523,7 +1523,7 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
 {
 	struct dev_ext_attribute *ea = to_ext_attr(attr);
 
-	return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
+	return scnprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
 }
 EXPORT_SYMBOL_GPL(device_show_bool);
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 6 lines · Denis Efremov
Quoted 1 line

From Documentation/filesystems/sysfs.txt:

- show() must not use snprintf() when formatting the value to be returned to user space. If you can guarantee that an overflow will never happen you can use sprintf() otherwise you must use scnprintf().

Quoted 6 lines

The reason is that snprintf() returns the length the resulting string would be, assuming the data will fit into the destination array. scnprintf() returns the length of the string actually created in buf.

device_show_{ulong,int,bool}() functions are reference implementation and should respect the kernel documentation.

Ok, but you are not following that documentation below, why not?

Quoted 15 lines

---

Actually, I don't know why snprintf() is used here initially. It looks safe to use sprintf().

 drivers/base/core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/base/core.c +0 -0
index 67d39a90b45c..2815deb511fc 100644

drivers/base/core.c ssize_t device_show_ulong(struct device *dev, · line 1475

Quoted 5 lines
drivers/base/core.c +1 -1
@@ -1472,7 +1472,7 @@ ssize_t device_show_ulong(struct device *dev,
 			  char *buf)
 {
 	struct dev_ext_attribute *ea = to_ext_attr(attr);
-	return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
+	return scnprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));

As the documentation states, these should just be sprintf(), if you really want to change these.

As-is, these are also ok, there is no need to change.

thanks,

greg k-h