Re: [Cocci] [PATCH] usb: atm: don't use snprintf() for sysfs attrs

From: Joe Perches
Date: Thu Aug 27 2020 - 17:44:08 EST


On Thu, 2020-08-27 at 23:36 +0200, Julia Lawall wrote:
> On Fri, 28 Aug 2020, Denis Efremov wrote:
[]
> Regarding current device_attr_show.cocci implementation, it detects the functions
> > by declaration:
> > ssize_t any_name(struct device *dev, struct device_attribute *attr, char *buf)
> >
> > and I limited the check to:
> > "return snprintf"
> > pattern because there are already too many warnings.
> >
> > Actually, it looks more correct to check for:
> > ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
> > {
> > <...
> > * snprintf@p(...);
> > ...>
> > }
> >
> > This pattern should also highlight the snprintf calls there we save returned
> > value in a var, e.g.:
> >
> > ret += snprintf(...);
> > ...
> > ret += snprintf(...);
> > ...
> > ret += snprintf(...);
> >
> > return ret;
> >
> > > Something like
> > >
> > > identifier f;
> > > fresh identifier = "sysfs" ## f;
> > >
> > > may be useful. Let me know if further help is needed.
> >
> > Initially, I wrote the rule to search for DEVICE_ATTR(..., ..., func_name, ...)
>
> This is what I would have expected.
>
> > functions. However, it looks like matching function prototype is enough. At least,
> > I failed to find false positives. I rejected the initial DEVICE_ATTR() searching
> > because I thought that it's impossible to handle DEVICE_ATTR_RO()/DEVICE_ATTR_RW()
> > macroses with coccinelle as they "generate" function names internally with
> > "##". "fresh identifier" should really help here, but now I doubt it's required in
> > device_attr_show.cocci, function prototype is enough.
>
> It's true that it is probably unique enough.

I tried:
@@
identifier f_show =~ "^.*_show$";
identifier dev, attr, buf;
const char *chr;
@@
ssize_t f_show(struct device *dev, struct device_attribute *attr, char
*buf)
{
<...
(
- sprintf
+ sysfs_sprintf
(...);
|
- snprintf(buf, PAGE_SIZE,
+ sysfs_sprintf(buf,
...);
|
- scnprintf(buf, PAGE_SIZE,
+ sysfs_sprintf(buf,
...);
|
strcpy(buf, chr);
sysfs_strcpy(buf, chr);
)
...>
}

which finds direct statements without an assign
but that doesn't find

arch/arm/common/dmabounce.c:static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr, char *buf)
arch/arm/common/dmabounce.c-{
arch/arm/common/dmabounce.c- struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
arch/arm/common/dmabounce.c- return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
arch/arm/common/dmabounce.c- device_info->small.allocs,
arch/arm/common/dmabounce.c- device_info->large.allocs,
arch/arm/common/dmabounce.c- device_info->total_allocs - device_info->small.allocs -
arch/arm/common/dmabounce.c- device_info->large.allocs,
arch/arm/common/dmabounce.c- device_info->total_allocs,
arch/arm/common/dmabounce.c- device_info->map_op_count,
arch/arm/common/dmabounce.c- device_info->bounce_count);
arch/arm/common/dmabounce.c-}