Re: [PATCH] atm: Convert sprintf/snprintf to sysfs_emit

From: Denis Kirjanov
Date: Thu Mar 14 2024 - 05:54:42 EST




On 3/14/24 11:44, Li Zhijian wrote:
> Per filesystems/sysfs.rst, show() should only use sysfs_emit()
> or sysfs_emit_at() when formatting the value to be returned to user space.
>
> coccinelle complains that there are still a couple of functions that use
> snprintf(). Convert them to sysfs_emit().
>
> sprintf() will be converted as weel if they have.
>
> Generally, this patch is generated by
> make coccicheck M=<path/to/file> MODE=patch \
> COCCI=scripts/coccinelle/api/device_attr_show.cocci
>
> No functional change intended

The patch should be targeted to net-next which is closed now.

>
> CC: Chas Williams <3chas3@xxxxxxxxx>
> CC: linux-atm-general@xxxxxxxxxxxxxxxxxxxxx
> CC: netdev@xxxxxxxxxxxxxxx
> Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx>
> ---
> This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
> Split them per subsystem so that the maintainer can review it easily
> [1] https://lore.kernel.org/lkml/20240116041129.3937800-1-lizhijian@xxxxxxxxxxx/
> ---
> drivers/atm/solos-pci.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/atm/solos-pci.c b/drivers/atm/solos-pci.c
> index d3c30a28c410..369a7f414f05 100644
> --- a/drivers/atm/solos-pci.c
> +++ b/drivers/atm/solos-pci.c
> @@ -198,8 +198,8 @@ static ssize_t solos_param_show(struct device *dev, struct device_attribute *att
>
> header = skb_put(skb, sizeof(*header));
>
> - buflen = snprintf((void *)&header[1], buflen - 1,
> - "L%05d\n%s\n", current->pid, attr->attr.name);
> + buflen = sysfs_emit((void *)&header[1], "L%05d\n%s\n", current->pid,
> + attr->attr.name);
> skb_put(skb, buflen);
>
> header->size = cpu_to_le16(buflen);
> @@ -453,7 +453,7 @@ static ssize_t console_show(struct device *dev, struct device_attribute *attr,
> skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
> spin_unlock_bh(&card->cli_queue_lock);
> if(skb == NULL)
> - return sprintf(buf, "No data.\n");
> + return sysfs_emit(buf, "No data.\n");
>
> len = skb->len;
> memcpy(buf, skb->data, len);
> @@ -548,7 +548,7 @@ static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
> data32 = ioread32(card->config_regs + GPIO_STATUS);
> data32 = (data32 >> gattr->offset) & 1;
>
> - return sprintf(buf, "%d\n", data32);
> + return sysfs_emit(buf, "%d\n", data32);
> }
>
> static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
> @@ -569,7 +569,7 @@ static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
> data32 = (data32 >> 5) & 0x0F;
> break;
> }
> - return sprintf(buf, "%d\n", data32);
> + return sysfs_emit(buf, "%d\n", data32);
> }
>
> static DEVICE_ATTR_RW(console);