Skip to contentSkip to conversation

Staging: fbtbt: show() methods should use scnprintf()

PATCH In review Ksenija Stanojevic 2 participants 4 messages Sep 29, 2015

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

PATCH 1/1 Staging: fbtbt: show() methods should use scnprintf()

snprintf() may return a value that exceeds its second argument, show() methods should use scnprintf() instead of snprintf().

---
 drivers/staging/fbtft/fbtft-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/fbtft/fbtft-sysfs.c +1 -1
index 8d8bd12..36a1f4e 100644
@@ -198,7 +198,7 @@ static ssize_t show_debug(struct device *device,
 	struct fb_info *fb_info = dev_get_drvdata(device);
 	struct fbtft_par *par = fb_info->par;
 
-	return snprintf(buf, PAGE_SIZE, "%lu\n", par->debug);
+	return scnprintf(buf, PAGE_SIZE, "%lu\n", par->debug);
 }
 
 static struct device_attribute debug_device_attr = \
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 13 lines · Ksenija Stanojevic

snprintf() may return a value that exceeds its second argument, show() methods should use scnprintf() instead of snprintf().

---
 drivers/staging/fbtft/fbtft-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/fbtft/fbtft-sysfs.c +0 -0
index 8d8bd12..36a1f4e 100644

drivers/staging/fbtft/fbtft-sysfs.c static ssize_t show_debug(struct device *device, · line 201

Quoted 5 lines
drivers/staging/fbtft/fbtft-sysfs.c +1 -1
@@ -198,7 +198,7 @@ static ssize_t show_debug(struct device *device,
 	struct fb_info *fb_info = dev_get_drvdata(device);
 	struct fbtft_par *par = fb_info->par;
 
-	return snprintf(buf, PAGE_SIZE, "%lu\n", par->debug);
+	return scnprintf(buf, PAGE_SIZE, "%lu\n", par->debug);

They should? Why? This shouldn't be needed at all, and really, a simple sprintf() is all that is necessary. I would just leave this alone, it's fine as-is.

thanks,

greg k-h

#2 lore raw

Hi,

Quoted 17 lines · Greg KH
Quoted 16 lines · Ksenija Stanojevic

snprintf() may return a value that exceeds its second argument, show() methods should use scnprintf() instead of snprintf().

---
 drivers/staging/fbtft/fbtft-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/fbtft/fbtft-sysfs.c +0 -0
index 8d8bd12..36a1f4e 100644
@@ -198,7 +198,7 @@ static ssize_t show_debug(struct device *device,
      struct fb_info *fb_info = dev_get_drvdata(device);
      struct fbtft_par *par = fb_info->par;
 
Quoted 6 lines

drivers/staging/fbtft/fbtft-sysfs.c static ssize_t show_debug(struct device *device, · line 201

Quoted 2 lines
drivers/staging/fbtft/fbtft-sysfs.c +1 -1
-     return snprintf(buf, PAGE_SIZE, "%lu\n", par->debug);
+     return scnprintf(buf, PAGE_SIZE, "%lu\n", par->debug);

They should? Why? This shouldn't be needed at all, and really, a simple sprintf() is all that is necessary. I would just leave this alone, it's fine as-is.

In kernel documentation :
https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
it states:

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()

Should I just use sprintf instead?

Thanks,
Ksenija

Quoted 10 lines

thanks,

greg k-h

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20150929193803.GA23361%40kroah.com.
For more options, visit https://groups.google.com/d/optout.

maintainer
#3 lore raw
Quoted 31 lines · Ksenija Stanojević

Hi,

Quoted 17 lines · Greg KH
Quoted 16 lines · Ksenija Stanojevic

snprintf() may return a value that exceeds its second argument, show() methods should use scnprintf() instead of snprintf().

---
 drivers/staging/fbtft/fbtft-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/fbtft/fbtft-sysfs.c +0 -0
index 8d8bd12..36a1f4e 100644
@@ -198,7 +198,7 @@ static ssize_t show_debug(struct device *device,
      struct fb_info *fb_info = dev_get_drvdata(device);
      struct fbtft_par *par = fb_info->par;
 
Quoted 6 lines

drivers/staging/fbtft/fbtft-sysfs.c static ssize_t show_debug(struct device *device, · line 201

Quoted 2 lines
drivers/staging/fbtft/fbtft-sysfs.c +1 -1
-     return snprintf(buf, PAGE_SIZE, "%lu\n", par->debug);
+     return scnprintf(buf, PAGE_SIZE, "%lu\n", par->debug);

They should? Why? This shouldn't be needed at all, and really, a simple sprintf() is all that is necessary. I would just leave this alone, it's fine as-is.

In kernel documentation :
https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
it states:

Quoted 6 lines

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()

Should I just use sprintf instead?

If you are writing new code for sysfs attributes like these, yes, use sprintf(). Don't modify existing code that is working just fine with no errors at all by trying to convert snprintf() to scnprintf() please.

thanks,

greg k-h