sh: dma-sysfs: use sysfs_emit{_at} in show functions
PATCH 1/3 sh: dma-sysfs: use sysfs_emit{_at} in show functions
Replace sprintf() with sysfs_emit() and sysfs_emit_at() in sysfs show functions. sysfs_emit() and sysfs_emit_at() are preferred for formatting sysfs output because they provide safer bounds checking.
--- arch/sh/drivers/dma/dma-sysfs.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/sh/drivers/dma/dma-sysfs.c +7 -6
@@ -13,6 +13,7 @@#include <linux/platform_device.h> #include <linux/err.h> #include <linux/string.h> +#include <linux/sysfs.h> #include <asm/dma.h> static const struct bus_type dma_subsys = {
@@ -33,9 +34,9 @@ static ssize_t dma_show_devices(struct device *dev,if (unlikely (!info) || !channel)continue ; - len +=sprintf (buf + len,"%2d: %14s %s\n" , - channel->chan, info->name, - channel->dev_id); + len +=sysfs_emit_at (buf, len,"%2d: %14s %s\n" , + channel->chan, info->name, + channel->dev_id); }return len;
@@ -65,7 +66,7 @@ static ssize_t dma_show_dev_id(struct device *dev, struct device_attribute *attr, char *buf) { struct dma_channel *channel = to_dma_channel(dev); -return sprintf (buf,"%s\n" , channel->dev_id); +return sysfs_emit (buf,"%s\n" , channel->dev_id); } static ssize_t dma_store_dev_id(struct device *dev,
@@ -98,7 +99,7 @@ static ssize_t dma_show_mode(struct device *dev, struct device_attribute *attr, char *buf) { struct dma_channel *channel = to_dma_channel(dev); -return sprintf (buf,"0x%08x\n" , channel->mode); +return sysfs_emit (buf,"0x%08x\n" , channel->mode); } static ssize_t dma_store_mode(struct device *dev,
@@ -117,7 +118,7 @@ static ssize_t dma_show_##field(struct device *dev, \ struct device_attribute *attr, char *buf)\ { \ struct dma_channel *channel = to_dma_channel(dev); \ -return sprintf (buf, fmt, channel->field); \ +return sysfs_emit (buf, fmt, channel->field); \ } \ static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
PATCH 2/3 sh: dma-sysfs: use strscpy in dma_store_dev_id
strcpy() has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy().
¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy
--- arch/sh/drivers/dma/dma-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/drivers/dma/dma-sysfs.c +1 -1
@@ -74,7 +74,7 @@ static ssize_t dma_store_dev_id(struct device *dev, const char *buf, size_t count) {struct dma_channel *channel =to_dma_channel (dev); -strcpy (channel->dev_id, buf); +strscpy (channel->dev_id, buf);return count; }
PATCH 3/3 sh: dma: drop redundant size argument from strscpy in request_dma
In reply to Thorsten BlumThe strscpy() size argument is optional if the destination buffer has a fixed length. Use the two-argument version of strscpy() to copy 'dev_id' to simplify the code.
--- arch/sh/drivers/dma/dma-api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/drivers/dma/dma-api.c +1 -1
@@ -96,7 +96,7 @@ int request_dma(unsigned int chan, const char *dev_id)if (atomic_xchg (&channel->busy,1 ))return -EBUSY ; -strscpy (channel->dev_id, dev_id,sizeof (channel->dev_id)); +strscpy (channel->dev_id, dev_id);if (info->ops->request) { result = info->ops->request (channel);
Re: [PATCH 2/3] sh: dma-sysfs: use strscpy in dma_store_dev_id
In reply to Thorsten BlumQuoted 7 lines · Thorsten Blum
strcpy()has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the saferstrscpy().¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy
Signed-off-by Thorsten Blum <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 3/3] sh: dma: drop redundant size argument from strscpy in request_dma
In reply to Thorsten BlumQuoted 5 lines · Thorsten Blum
The
strscpy()size argument is optional if the destination buffer has a fixed length. Use the two-argument version ofstrscpy()to copy 'dev_id' to simplify the code.Signed-off-by Thorsten Blum <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 5 lines · Thorsten Blum
Replace
sprintf()withsysfs_emit()andsysfs_emit_at()in sysfs show functions.sysfs_emit()andsysfs_emit_at()are preferred for formatting sysfs output because they provide safer bounds checking.Signed-off-by Thorsten Blum <[email protected]>
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds