diff mbox series

new checkpatch flexible array test ? (was Re: [PATCH v4 12/14] gunyah: rsc_mgr: Add RPC for console services)

Message ID c7e45416cc911290efb5ad669f4a45bdc5678f69.camel@perches.com (mailing list archive)
State Not Applicable
Headers show
Series new checkpatch flexible array test ? (was Re: [PATCH v4 12/14] gunyah: rsc_mgr: Add RPC for console services) | expand

Commit Message

Joe Perches Oct. 3, 2022, 1:46 a.m. UTC
On Fri, 2022-09-30 at 14:22 +0200, Greg Kroah-Hartman wrote:
> On Wed, Sep 28, 2022 at 12:56:31PM -0700, Elliot Berman wrote:
> > Gunyah resource manager defines a simple API for virtual machine log
> > sharing with the console service.
[]
> > diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h
[]
> > +struct gh_rm_notif_vm_console_chars {
> > +	u16 vmid;
> > +	u16 num_bytes;
> > +	u8 bytes[0];
> 
> Please do not use [0] for new structures, otherwise we will just have to
> fix them up again as we are trying to get rid of all of these from the
> kernel. Just use "bytes[];" instead.

Maybe a checkpatch addition like:
---
 scripts/checkpatch.pl | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Greg Kroah-Hartman Oct. 3, 2022, 5:29 a.m. UTC | #1
On Sun, Oct 02, 2022 at 06:46:30PM -0700, Joe Perches wrote:
> On Fri, 2022-09-30 at 14:22 +0200, Greg Kroah-Hartman wrote:
> > On Wed, Sep 28, 2022 at 12:56:31PM -0700, Elliot Berman wrote:
> > > Gunyah resource manager defines a simple API for virtual machine log
> > > sharing with the console service.
> []
> > > diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h
> []
> > > +struct gh_rm_notif_vm_console_chars {
> > > +	u16 vmid;
> > > +	u16 num_bytes;
> > > +	u8 bytes[0];
> > 
> > Please do not use [0] for new structures, otherwise we will just have to
> > fix them up again as we are trying to get rid of all of these from the
> > kernel. Just use "bytes[];" instead.
> 
> Maybe a checkpatch addition like:
> ---
>  scripts/checkpatch.pl | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 2737e4ced5745..187ed84c1f80a 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3948,6 +3948,17 @@ sub process {
>  			}
>  		}
>  
> +# check for zero length array declarations in likely structs
> +		if ($line =~ /^\+\t($Declare\s*$Ident)\s*\[\s*0\s*\]\s*;\s*$/ &&
> +		    defined $lines[$linenr] &&
> +		    $lines[$linenr] =~ /^[\+ ]\}\s*(?:__\w+\s*(?:$balanced_parens)?)\s*;\s*$/) {
> +			if (WARN("FLEXIBLE_ARRAY_ZERO",
> +				 "Prefer flexible length array declarations with [] over [0]\n" . $herecurr) &&
> +			    $fix) {
> +				$fixed[$fixlinenr] =~ s/\[\s*0\s*\]/[]/;
> +			}
> +		}
> +
>  # check for multiple consecutive blank lines
>  		if ($prevline =~ /^[\+ ]\s*$/ &&
>  		    $line =~ /^\+\s*$/ &&

This is a question for Gustavo, who did all the work here.  Gustavo,
does the above checkpatch change look good to you?

thanks,

greg k-h
Joe Perches Oct. 3, 2022, 5:38 a.m. UTC | #2
On Mon, 2022-10-03 at 07:29 +0200, Greg Kroah-Hartman wrote:
> On Sun, Oct 02, 2022 at 06:46:30PM -0700, Joe Perches wrote:
> > On Fri, 2022-09-30 at 14:22 +0200, Greg Kroah-Hartman wrote:
> > > On Wed, Sep 28, 2022 at 12:56:31PM -0700, Elliot Berman wrote:
> > > > Gunyah resource manager defines a simple API for virtual machine log
> > > > sharing with the console service.
> > []
> > > > diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h
> > []
> > > > +struct gh_rm_notif_vm_console_chars {
> > > > +	u16 vmid;
> > > > +	u16 num_bytes;
> > > > +	u8 bytes[0];
> > > 
> > > Please do not use [0] for new structures, otherwise we will just have to
> > > fix them up again as we are trying to get rid of all of these from the
> > > kernel. Just use "bytes[];" instead.
> > 
> > Maybe a checkpatch addition like:
> > ---
[]
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> > @@ -3948,6 +3948,17 @@ sub process {
> >  			}
> >  		}
> >  
> > +# check for zero length array declarations in likely structs
> > +		if ($line =~ /^\+\t($Declare\s*$Ident)\s*\[\s*0\s*\]\s*;\s*$/ &&
> > +		    defined $lines[$linenr] &&
> > +		    $lines[$linenr] =~ /^[\+ ]\}\s*(?:__\w+\s*(?:$balanced_parens)?)\s*;\s*$/) {

This should actually be:

		    $lines[$linenr] =~ /^[\+ ]\}(?:\s*__\w+\s*(?:$balanced_parens)?)*\s*;\s*$/) {

as it was missing a * for uses like

	int foo[0];
} __packed __aligned(4);

and uses without any attribute at all

> > +			if (WARN("FLEXIBLE_ARRAY_ZERO",
> > +				 "Prefer flexible length array declarations with [] over [0]\n" . $herecurr) &&
> > +			    $fix) {
> > +				$fixed[$fixlinenr] =~ s/\[\s*0\s*\]/[]/;
> > +			}
> > +		}
> > +
> >  # check for multiple consecutive blank lines
> >  		if ($prevline =~ /^[\+ ]\s*$/ &&
> >  		    $line =~ /^\+\s*$/ &&
> 
> This is a question for Gustavo, who did all the work here.  Gustavo,
> does the above checkpatch change look good to you?
Gustavo A. R. Silva Oct. 31, 2022, 6:23 p.m. UTC | #3
On 10/3/22 00:38, Joe Perches wrote:
> On Mon, 2022-10-03 at 07:29 +0200, Greg Kroah-Hartman wrote:
>> On Sun, Oct 02, 2022 at 06:46:30PM -0700, Joe Perches wrote:
>>> On Fri, 2022-09-30 at 14:22 +0200, Greg Kroah-Hartman wrote:
>>>> On Wed, Sep 28, 2022 at 12:56:31PM -0700, Elliot Berman wrote:
>>>>> Gunyah resource manager defines a simple API for virtual machine log
>>>>> sharing with the console service.
>>> []
>>>>> diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h
>>> []
>>>>> +struct gh_rm_notif_vm_console_chars {
>>>>> +	u16 vmid;
>>>>> +	u16 num_bytes;
>>>>> +	u8 bytes[0];
>>>>
>>>> Please do not use [0] for new structures, otherwise we will just have to
>>>> fix them up again as we are trying to get rid of all of these from the
>>>> kernel. Just use "bytes[];" instead.
>>>
>>> Maybe a checkpatch addition like:
>>> ---
> []
>>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
>>> @@ -3948,6 +3948,17 @@ sub process {
>>>   			}
>>>   		}
>>>   
>>> +# check for zero length array declarations in likely structs
>>> +		if ($line =~ /^\+\t($Declare\s*$Ident)\s*\[\s*0\s*\]\s*;\s*$/ &&
>>> +		    defined $lines[$linenr] &&
>>> +		    $lines[$linenr] =~ /^[\+ ]\}\s*(?:__\w+\s*(?:$balanced_parens)?)\s*;\s*$/) {

This sounds great. We need the same for one-element arrays. :)
Both zero-length and one-element arrays are deprecated.

> 
> This should actually be:
> 
> 		    $lines[$linenr] =~ /^[\+ ]\}(?:\s*__\w+\s*(?:$balanced_parens)?)*\s*;\s*$/) {

I agree. Thanks.

> 
> as it was missing a * for uses like
> 
> 	int foo[0];
> } __packed __aligned(4);
> 
> and uses without any attribute at all
> 
>>> +			if (WARN("FLEXIBLE_ARRAY_ZERO",
>>> +				 "Prefer flexible length array declarations with [] over [0]\n" . $herecurr) &&
>>> +			    $fix) {
>>> +				$fixed[$fixlinenr] =~ s/\[\s*0\s*\]/[]/;
>>> +			}
>>> +		}
>>> +
>>>   # check for multiple consecutive blank lines
>>>   		if ($prevline =~ /^[\+ ]\s*$/ &&
>>>   		    $line =~ /^\+\s*$/ &&
>>
>> This is a question for Gustavo, who did all the work here.  Gustavo,
>> does the above checkpatch change look good to you?

Yep; the idea is great. :)

Another alternative to stop those fake flex-arrays from entering the codebase
is to run Coccinelle scripts during linux-next builds, as suggested by Elena
Reshetova at LSSEU a couple of months ago.

However, if these can be stopped with checkpatch it'd be really helpful, as well.

Thanks
--
Gustavo
diff mbox series

Patch

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2737e4ced5745..187ed84c1f80a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3948,6 +3948,17 @@  sub process {
 			}
 		}
 
+# check for zero length array declarations in likely structs
+		if ($line =~ /^\+\t($Declare\s*$Ident)\s*\[\s*0\s*\]\s*;\s*$/ &&
+		    defined $lines[$linenr] &&
+		    $lines[$linenr] =~ /^[\+ ]\}\s*(?:__\w+\s*(?:$balanced_parens)?)\s*;\s*$/) {
+			if (WARN("FLEXIBLE_ARRAY_ZERO",
+				 "Prefer flexible length array declarations with [] over [0]\n" . $herecurr) &&
+			    $fix) {
+				$fixed[$fixlinenr] =~ s/\[\s*0\s*\]/[]/;
+			}
+		}
+
 # check for multiple consecutive blank lines
 		if ($prevline =~ /^[\+ ]\s*$/ &&
 		    $line =~ /^\+\s*$/ &&