Re: [PATCH v9 01/27] kallsyms: use `sizeof` instead of hardcoded size

From: Kees Cook
Date: Thu Aug 18 2022 - 12:03:34 EST


On Thu, Aug 18, 2022 at 12:03:37PM +0300, Konstantin Shelekhin wrote:
> On Wed, Aug 17, 2022 at 12:36:33PM -0700, Kees Cook wrote:
> > On Sat, Aug 06, 2022 at 01:40:33AM +0300, Konstantin Shelekhin wrote:
> > > > diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
> > > > index f18e6dfc68c5..52f5488c61bc 100644
> > > > --- a/scripts/kallsyms.c
> > > > +++ b/scripts/kallsyms.c
> > > > @@ -206,7 +206,7 @@ static struct sym_entry *read_symbol(FILE *in)
> > > >
> > > > rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
> > > > if (rc != 3) {
> > > > - if (rc != EOF && fgets(name, 500, in) == NULL)
> > > > + if (rc != EOF && fgets(name, sizeof(name), in) == NULL)
> > > > fprintf(stderr, "Read error or end of file.\n");
> > > > return NULL;
> > > > }
> > >
> > > Might be another nit, but IMO it's better to use ARRAY_SIZE() here.
> >
> > I'm not sure I see a benefit for char arrays. It'll produce the same
> > result, and the tradition for string functions is to use sizeof().
> > *shrug*
>
> ARRAY_SIZE() (though not this one) can catch this:
>
> - char array[16];
> + char *array;
>
> Saves me some.

Oh, that's an excellent point; I forgot it'll actually compile-time
error if the var is a pointer. +1

--
Kees Cook