Re: [Fwd: [PATCH 1/2] string: Add stracpy and stracpy_pad mechanisms]

Joe Perches
2019-07-23T23:42:25Z

On Tue, 2019-07-23 at 15:52 -0500, Julia Lawall wrote:
> On Mon, 22 Jul 2019, Joe Perches wrote:
> > I just sent a patch to add yet another string copy mechanism.
> >
> > This could help avoid misuses of strscpy and strlcpy like this
> > patch set:
> >
> > https://lore.kernel.org/lkml/cover.1562283944.git.joe@perches.com/T/
> >
> > A possible cocci script to do conversions could be:
> >
> > $ cat str.cpy.cocci
> > @@
> > expression e1;
> > expression e2;
> > @@
> >
> > - strscpy(e1, e2, sizeof(e1))
> > + stracpy(e1, e2)
> >
> > @@
> > expression e1;
> > expression e2;
> > @@
> >
> > - strlcpy(e1, e2, sizeof(e1))
> > + stracpy(e1, e2)
> >
> > This obviously does not match the style of all the
> > scripts/coccinelle cocci files, but this might be
> > something that could be added improved and added.
> >
> > This script produces:
> >
> > $ spatch --in-place -sp-file str.cpy.cocci .
> > $ git checkout tools/
> > $ git diff --shortstat
> > 958 files changed, 2179 insertions(+), 2655 deletions(-)
> >
> > The remainder of strlcpy and strscpy uses in the
> > kernel would generally have a form like:
> >
> > strlcpy(to, from, DEFINE)
> >
> > where DEFINE is the specified size of to
> >
> > Could the cocci script above be updated to find
> > and correct those styles as well?
>
> I guess it would depend on what "to" is and what DEFINE expands into. For
> example, in cpuidle-powernv.c, I see:
>
> strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
>
> and by poking around I see:
>
> struct cpuidle_state {
> char name[CPUIDLE_NAME_LEN];
> char desc[CPUIDLE_DESC_LEN];
> ...
> };

Yes, ideally this case would not modify the #define for the
length but adapt the strlcpy(,,DEFINE)

There are a lot of these in drivers/hwmon using I2C_NAME_SIZE.

> I will look into it.

Thanks.