Re: [PATCH] virtio-blk: fixup coccinelle warnings

From: Stefan Hajnoczi
Date: Thu Oct 21 2021 - 05:15:37 EST


On Thu, Oct 21, 2021 at 12:08:23AM -0700, Joe Perches wrote:
> On Thu, 2021-10-21 at 06:51 +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 or sprintf makes more sense.
> []
> > diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
> []
> > @@ -624,7 +624,7 @@ cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
> > - return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
> > + return sysfs_emit(buf, "%s\n", virtblk_cache_types[writeback]);
>
> Perhaps scripts/coccinelle/api/device_attr_show.cocci should be updated
> to be more like the script used in commit 1c7fd72687d6

This won't catch the case covered by the patch because it's
"snprintf(buf, 40" instead of "snprintf(buf, PAGE_SIZE", although
any size that's not PAGE_SIZE needs to be reviewed carefully in case the
intent of the statement is to truncate the output.

Stefan

Attachment: signature.asc
Description: PGP signature