2024-03-14 09:16:05

by Zhijian Li (Fujitsu)

[permalink] [raw]
Subject: [PATCH] mmc: core: Convert sprintf/snprintf to sysfs_emit

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

CC: Ulf Hansson <[email protected]>
CC: [email protected]
Signed-off-by: Li Zhijian <[email protected]>
---
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/[email protected]/
---
drivers/mmc/core/block.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
index 64a3492e8002..1e1e136d9e72 100644
--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -234,7 +234,7 @@ static ssize_t power_ro_lock_show(struct device *dev,
else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
locked = 1;

- ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
+ ret = sysfs_emit(buf, "%d\n", locked);

mmc_blk_put(md);

@@ -296,9 +296,9 @@ static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
int ret;
struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));

- ret = snprintf(buf, PAGE_SIZE, "%d\n",
- get_disk_ro(dev_to_disk(dev)) ^
- md->read_only);
+ ret = sysfs_emit(buf, "%d\n",
+ get_disk_ro(dev_to_disk(dev)) ^
+ md->read_only);
mmc_blk_put(md);
return ret;
}
--
2.29.2



2024-03-25 16:05:05

by Ulf Hansson

[permalink] [raw]
Subject: Re: [PATCH] mmc: core: Convert sprintf/snprintf to sysfs_emit

On Thu, 14 Mar 2024 at 10:15, Li Zhijian <[email protected]> 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
>
> CC: Ulf Hansson <[email protected]>
> CC: [email protected]
> Signed-off-by: Li Zhijian <[email protected]>

Applied for next, thanks!

Kind regards
Uffe


> ---
> 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/[email protected]/
> ---
> drivers/mmc/core/block.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
> index 64a3492e8002..1e1e136d9e72 100644
> --- a/drivers/mmc/core/block.c
> +++ b/drivers/mmc/core/block.c
> @@ -234,7 +234,7 @@ static ssize_t power_ro_lock_show(struct device *dev,
> else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
> locked = 1;
>
> - ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
> + ret = sysfs_emit(buf, "%d\n", locked);
>
> mmc_blk_put(md);
>
> @@ -296,9 +296,9 @@ static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
> int ret;
> struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
>
> - ret = snprintf(buf, PAGE_SIZE, "%d\n",
> - get_disk_ro(dev_to_disk(dev)) ^
> - md->read_only);
> + ret = sysfs_emit(buf, "%d\n",
> + get_disk_ro(dev_to_disk(dev)) ^
> + md->read_only);
> mmc_blk_put(md);
> return ret;
> }
> --
> 2.29.2
>