Skip to contentSkip to conversation

staging: fieldbus: replace snprintf in show functions with sysfs_emit

PATCH Superseded Deepak R Varma 2 participants 3 messages Nov 9, 2022 Staging

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: fieldbus: replace snprintf in show functions with sysfs_emit

The show() methods should only use sysfs_emit() when formatting values to be returned to the user space.
Ref: Documentation/filesystems/sysfs.rst
Issue identified by coccicheck.

---

Changes in v2:
1. Switch to using sysfs_emit instead of scnprintf
2. Update patch subject and log accordingly Feedback from [email protected]

 drivers/staging/fieldbus/dev_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/fieldbus/dev_core.c +3 -3
index 5aab734606ea..d32599f4eb44 100644
@@ -67,10 +67,10 @@ static ssize_t card_name_show(struct device *dev, struct device_attribute *attr,
 	struct fieldbus_dev *fb = dev_get_drvdata(dev);
 
 	/*
-	 * card_name was provided by child driver, could potentially be long.
-	 * protect against buffer overrun.
+	 * sysfs provides PAGE_SIZE long buffer to take care of potentially
+	 * long card_name provided by child driver.
 	 */
-	return snprintf(buf, PAGE_SIZE, "%s\n", fb->card_name);
+	return sysfs_emit(buf, "%s\n", fb->card_name);
 }
 static DEVICE_ATTR_RO(card_name);
 

--
2.34.1

#1 lore raw
Quoted 22 lines · Deepak R Varma

The show() methods should only use sysfs_emit() when formatting values to be returned to the user space.
Ref: Documentation/filesystems/sysfs.rst
Issue identified by coccicheck.

---

Changes in v2:
1. Switch to using sysfs_emit instead of scnprintf
2. Update patch subject and log accordingly Feedback from [email protected]

 drivers/staging/fieldbus/dev_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/fieldbus/dev_core.c +0 -0
index 5aab734606ea..d32599f4eb44 100644

drivers/staging/fieldbus/dev_core.c static ssize_t card_name_show(struct device *dev, struct device_attribute *attr, · line 70

Quoted 6 lines
drivers/staging/fieldbus/dev_core.c +1 -2
@@ -67,10 +67,10 @@ static ssize_t card_name_show(struct device *dev, struct device_attribute *attr,
 	struct fieldbus_dev *fb = dev_get_drvdata(dev);
 
 	/*
-	 * card_name was provided by child driver, could potentially be long.
-	 * protect against buffer overrun.
+	 * sysfs provides PAGE_SIZE long buffer to take care of potentially

No need to ever mention PAGE_SIZE at all, this comment should just be:

	/* card_name was provided by child driver */

But the larger question is, why did you only convert one of the sysfs show functions in this file? Why not do them all?

thanks,

greg k-h

#2 lore raw
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines · Deepak R Varma

The show() methods should only use sysfs_emit() when formatting values to be returned to the user space.
Ref: Documentation/filesystems/sysfs.rst
Issue identified by coccicheck.

Quoted 8 lines

drivers/staging/fieldbus/dev_core.c static ssize_t card_name_show(struct device *dev, struct device_attribute *attr, · line 70

Quoted 5 lines
drivers/staging/fieldbus/dev_core.c +1 -2
 	/*
-	 * card_name was provided by child driver, could potentially be long.
-	 * protect against buffer overrun.
+	 * sysfs provides PAGE_SIZE long buffer to take care of potentially

No need to ever mention PAGE_SIZE at all, this comment should just be:

	/* card_name was provided by child driver */

Sure. I will update the comment in the revision.

Quoted 3 lines

But the larger question is, why did you only convert one of the sysfs show functions in this file? Why not do them all?

I was limiting it to what is reported by coccicheck. I will review other such show functions and send a consolidated patch.

Thank you,
./drv

Quoted 5 lines

thanks,

greg k-h