Re: [PATCH v3] checkpatch: Check for 0-length and 1-element arrays

From: Kees Cook
Date: Tue May 30 2023 - 20:41:38 EST


On Fri, May 26, 2023 at 09:31:39PM -0700, Joe Perches wrote:
> On Fri, 2023-05-26 at 19:09 -0700, Kees Cook wrote:
> > Fake flexible arrays have been deprecated since last millennium. Proper
> > C99 flexible arrays must be used throughout the kernel so
> > CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array
> > bounds checking.
>
> Hi again.
>
>
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -7430,6 +7430,21 @@ sub process {
> > }
> > }
> >
> > +# check for array definition/declarations that should use flexible arrays instead
> > + if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ &&
> > + $prevline =~ /^\+\s*(?:\}|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
>
> Ahh, now I see why you want to use (?:\}|$Type) to find
> struct arrays defined as the last object in another struct.

Yeah -- this has been a place where past "[1]"s have snuck in before. :)

> grepping for some existing variants, perhaps the $sline test should
> be extended to something like:
>
> if ($sline =~ /^[\+ ]\s*}\s*(?:__packed|__attribute__\s*$balanced_parens)?\s*;\s*$/ &&

Ugh, I forgot attributes. Hmmm. I don't need this check to be _perfect_
either. :P Really only "__packed" has come up in other conversions.

> > + if ($1 == '0') {
> > + if (ERROR("ZERO_LENGTH_ARRAY",
> > + "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n"; . $hereprev) &&
>
> Instead of github links, how about using:
> https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays

Yeah, though the issues have a bit more detail. Probably better to be
self-referential. :)

>
>
> Why not combine both of these into a single ERROR?
> What's the value in separate types?

Yup, with the same URL (instead of separate URLs), it gets cleaner too.

Thanks!

-Kees

--
Kees Cook