Re: [PATCH] char: ipmi: replace snprintf in show functions with sysfs_emit

From: Corey Minyard
Date: Thu Oct 21 2021 - 07:55:07 EST


On Thu, Oct 21, 2021 at 11:06:08AM +0000, cgel.zte@xxxxxxxxx wrote:
> From: Ye Guojin <ye.guojin@xxxxxxxxxx>
>
> coccicheck complains about the use of snprintf() in sysfs show
> functions:
> WARNING use scnprintf or sprintf
>
> Use sysfs_emit instead of scnprintf, snprintf or sprintf makes more
> sense.

Yes, I agree. It's in my queue.

Thanks,

-corey

>
> Reported-by: Zeal Robot <zealci@xxxxxxxxxx>
> Signed-off-by: Ye Guojin <ye.guojin@xxxxxxxxxx>
> ---
> drivers/char/ipmi/ipmi_msghandler.c | 20 ++++++++++----------
> drivers/char/ipmi/ipmi_si_intf.c | 8 ++++----
> drivers/char/ipmi/ipmi_ssif.c | 4 ++--
> 3 files changed, 16 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
> index a60201d3f735..deed355422f4 100644
> --- a/drivers/char/ipmi/ipmi_msghandler.c
> +++ b/drivers/char/ipmi/ipmi_msghandler.c
> @@ -2716,7 +2716,7 @@ static ssize_t device_id_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 10, "%u\n", id.device_id);
> + return sysfs_emit(buf, "%u\n", id.device_id);
> }
> static DEVICE_ATTR_RO(device_id);
>
> @@ -2732,7 +2732,7 @@ static ssize_t provides_device_sdrs_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
> + return sysfs_emit(buf, "%u\n", (id.device_revision & 0x80) >> 7);
> }
> static DEVICE_ATTR_RO(provides_device_sdrs);
>
> @@ -2747,7 +2747,7 @@ static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
> if (rv)
> return rv;
>
> - return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
> + return sysfs_emit(buf, "%u\n", id.device_revision & 0x0F);
> }
> static DEVICE_ATTR_RO(revision);
>
> @@ -2763,7 +2763,7 @@ static ssize_t firmware_revision_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
> + return sysfs_emit(buf, "%u.%x\n", id.firmware_revision_1,
> id.firmware_revision_2);
> }
> static DEVICE_ATTR_RO(firmware_revision);
> @@ -2780,7 +2780,7 @@ static ssize_t ipmi_version_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 20, "%u.%u\n",
> + return sysfs_emit(buf, "%u.%u\n",
> ipmi_version_major(&id),
> ipmi_version_minor(&id));
> }
> @@ -2798,7 +2798,7 @@ static ssize_t add_dev_support_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
> + return sysfs_emit(buf, "0x%02x\n", id.additional_device_support);
> }
> static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
> NULL);
> @@ -2815,7 +2815,7 @@ static ssize_t manufacturer_id_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
> + return sysfs_emit(buf, "0x%6.6x\n", id.manufacturer_id);
> }
> static DEVICE_ATTR_RO(manufacturer_id);
>
> @@ -2831,7 +2831,7 @@ static ssize_t product_id_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
> + return sysfs_emit(buf, "0x%4.4x\n", id.product_id);
> }
> static DEVICE_ATTR_RO(product_id);
>
> @@ -2847,7 +2847,7 @@ static ssize_t aux_firmware_rev_show(struct device *dev,
> if (rv)
> return rv;
>
> - return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
> + return sysfs_emit(buf, "0x%02x 0x%02x 0x%02x 0x%02x\n",
> id.aux_firmware_revision[3],
> id.aux_firmware_revision[2],
> id.aux_firmware_revision[1],
> @@ -2869,7 +2869,7 @@ static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
> if (!guid_set)
> return -ENOENT;
>
> - return snprintf(buf, UUID_STRING_LEN + 1 + 1, "%pUl\n", &guid);
> + return sysfs_emit(buf, "%pUl\n", &guid);
> }
> static DEVICE_ATTR_RO(guid);
>
> diff --git a/drivers/char/ipmi/ipmi_si_intf.c b/drivers/char/ipmi/ipmi_si_intf.c
> index 6f3272b58ced..64dedb3ef8ec 100644
> --- a/drivers/char/ipmi/ipmi_si_intf.c
> +++ b/drivers/char/ipmi/ipmi_si_intf.c
> @@ -1603,7 +1603,7 @@ static ssize_t name##_show(struct device *dev, \
> { \
> struct smi_info *smi_info = dev_get_drvdata(dev); \
> \
> - return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name)); \
> + return sysfs_emit(buf, "%u\n", smi_get_stat(smi_info, name)); \
> } \
> static DEVICE_ATTR_RO(name)
>
> @@ -1613,7 +1613,7 @@ static ssize_t type_show(struct device *dev,
> {
> struct smi_info *smi_info = dev_get_drvdata(dev);
>
> - return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
> + return sysfs_emit(buf, "%s\n", si_to_str[smi_info->io.si_type]);
> }
> static DEVICE_ATTR_RO(type);
>
> @@ -1624,7 +1624,7 @@ static ssize_t interrupts_enabled_show(struct device *dev,
> struct smi_info *smi_info = dev_get_drvdata(dev);
> int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
>
> - return snprintf(buf, 10, "%d\n", enabled);
> + return sysfs_emit(buf, "%d\n", enabled);
> }
> static DEVICE_ATTR_RO(interrupts_enabled);
>
> @@ -1646,7 +1646,7 @@ static ssize_t params_show(struct device *dev,
> {
> struct smi_info *smi_info = dev_get_drvdata(dev);
>
> - return snprintf(buf, 200,
> + return sysfs_emit(buf,
> "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
> si_to_str[smi_info->io.si_type],
> addr_space_to_str[smi_info->io.addr_space],
> diff --git a/drivers/char/ipmi/ipmi_ssif.c b/drivers/char/ipmi/ipmi_ssif.c
> index 20d5af92966d..0c62e578749e 100644
> --- a/drivers/char/ipmi/ipmi_ssif.c
> +++ b/drivers/char/ipmi/ipmi_ssif.c
> @@ -1190,7 +1190,7 @@ static ssize_t ipmi_##name##_show(struct device *dev, \
> { \
> struct ssif_info *ssif_info = dev_get_drvdata(dev); \
> \
> - return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
> + return sysfs_emit(buf, "%u\n", ssif_get_stat(ssif_info, name));\
> } \
> static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
>
> @@ -1198,7 +1198,7 @@ static ssize_t ipmi_type_show(struct device *dev,
> struct device_attribute *attr,
> char *buf)
> {
> - return snprintf(buf, 10, "ssif\n");
> + return sysfs_emit(buf, "ssif\n");
> }
> static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
>
> --
> 2.25.1
>