2022-11-04 17:22:47

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH -next v2] checkpatch: Add check for array allocator family argument order

Le 04/11/2022 à 08:05, Liao Chang a écrit :
> These array allocator family are sometimes misused with the first and
> second arguments switchted.
>
> Same issue with calloc, kvcalloc, kvmalloc_array etc.
>
> Bleat if sizeof is the first argument.
>
> Link: https://lore.kernel.org/lkml/[email protected]/
> Signed-off-by: Liao Chang <[email protected]>
> Acked-by: Joe Perches <[email protected]>
> ---
> v2:
> 1. Acked-by Joe Perches.
> 2. Use lore links in Link tag.
>
> ---
> scripts/checkpatch.pl | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 1e5e66ae5a52..a9a9dc277cff 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7128,7 +7128,7 @@ sub process {
> }
>
> # check for alloc argument mismatch
> - if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
> + if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
> WARN("ALLOC_ARRAY_ARGS",
> "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
> }

Hi,

Should the devm_ and not devm_ cases be separated?

In the devm_case, sizeof will never be just after the first '('.

CJ


2022-11-05 18:08:15

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH -next v2] checkpatch: Add check for array allocator family argument order

On Fri, 2022-11-04 at 18:08 +0100, Christophe JAILLET wrote:
> Le 04/11/2022 ? 08:05, Liao Chang a ?crit?:
> > These array allocator family are sometimes misused with the first and
> > second arguments switchted.
> >
> > Same issue with calloc, kvcalloc, kvmalloc_array etc.
> >
> > Bleat if sizeof is the first argument.
> >
> > Link: https://lore.kernel.org/lkml/[email protected]/
> > Signed-off-by: Liao Chang <[email protected]>
> > Acked-by: Joe Perches <[email protected]>
> > ---
> > v2:
> > 1. Acked-by Joe Perches.
> > 2. Use lore links in Link tag.
> >
> > ---
> > scripts/checkpatch.pl | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > index 1e5e66ae5a52..a9a9dc277cff 100755
> > --- a/scripts/checkpatch.pl
> > +++ b/scripts/checkpatch.pl
> > @@ -7128,7 +7128,7 @@ sub process {
> > }
> >
> > # check for alloc argument mismatch
> > - if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
> > + if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
> > WARN("ALLOC_ARRAY_ARGS",
> > "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
> > }
>
> Hi,
>
> Should the devm_ and not devm_ cases be separated?
>
> In the devm_case, sizeof will never be just after the first '('.

Right.

Likely this works better:
---
scripts/checkpatch.pl | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7be93c3df2bcb..7f37976a9f8b5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7145,12 +7145,18 @@ sub process {
"Reusing the krealloc arg is almost always a bug\n" . $herecurr);
}

-# check for alloc argument mismatch
- if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
+# check for alloc argument mismatch in alloc functions
+ if ($line =~ /\b(((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
WARN("ALLOC_ARRAY_ARGS",
"$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
}

+# check for alloc argument mismatch in devm_ alloc functions
+ if ($line =~ /\b(devm_((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*$FuncArg\s*,\s*sizeof\b/) {
+ WARN("ALLOC_ARRAY_ARGS",
+ "$1 uses number as second arg, sizeof is generally wrong\n" . $herecurr);
+ }
+
# check for multiple semicolons
if ($line =~ /;\s*;\s*$/) {
if (WARN("ONE_SEMICOLON",


2022-11-05 21:48:21

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH -next v2] checkpatch: Add check for array allocator family argument order


Le 05/11/2022 à 18:24, Joe Perches a écrit :
> On Fri, 2022-11-04 at 18:08 +0100, Christophe JAILLET wrote:
>> Le 04/11/2022 à 08:05, Liao Chang a écrit :
>>> These array allocator family are sometimes misused with the first and
>>> second arguments switchted.
>>>
>>> Same issue with calloc, kvcalloc, kvmalloc_array etc.
>>>
>>> Bleat if sizeof is the first argument.
>>>
>>> Link: https://lore.kernel.org/lkml/[email protected]/
>>> Signed-off-by: Liao Chang <[email protected]>
>>> Acked-by: Joe Perches <[email protected]>
>>> ---
>>> v2:
>>> 1. Acked-by Joe Perches.
>>> 2. Use lore links in Link tag.
>>>
>>> ---
>>> scripts/checkpatch.pl | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
>>> index 1e5e66ae5a52..a9a9dc277cff 100755
>>> --- a/scripts/checkpatch.pl
>>> +++ b/scripts/checkpatch.pl
>>> @@ -7128,7 +7128,7 @@ sub process {
>>> }
>>>
>>> # check for alloc argument mismatch
>>> - if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
>>> + if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
>>> WARN("ALLOC_ARRAY_ARGS",
>>> "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
>>> }
>> Hi,
>>
>> Should the devm_ and not devm_ cases be separated?
>>
>> In the devm_case, sizeof will never be just after the first '('.
> Right.
>
> Likely this works better:
> ---
> scripts/checkpatch.pl | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 7be93c3df2bcb..7f37976a9f8b5 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7145,12 +7145,18 @@ sub process {
> "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
> }
>
> -# check for alloc argument mismatch
> - if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
> +# check for alloc argument mismatch in alloc functions
> + if ($line =~ /\b(((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
> WARN("ALLOC_ARRAY_ARGS",
> "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
> }
>
> +# check for alloc argument mismatch in devm_ alloc functions
> + if ($line =~ /\b(devm_((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*$FuncArg\s*,\s*sizeof\b/) {
> + WARN("ALLOC_ARRAY_ARGS",
> + "$1 uses number as second arg, sizeof is generally wrong\n" . $herecurr);
> + }
> +
> # check for multiple semicolons
> if ($line =~ /;\s*;\s*$/) {
> if (WARN("ONE_SEMICOLON",
>
+1