2007-08-08 02:45:04

by Dave Jones

[permalink] [raw]
Subject: Make checkpatch warn about pointless casting of kalloc returns.

Make checkpatch warn about pointless casting of kalloc returns.

Signed-off-by: Dave Jones <[email protected]>

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 73751ab..c6cae6a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1051,6 +1051,11 @@ sub process {
CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
}
}
+
+# check for pointless casting of kmalloc return
+ if ($rawline =~ /\*\)[ ]k[czm]alloc/) {
+ WARN("No need to cast return value.\n");
+ }
}

if ($chk_patch && !$is_patch) {
--
http://www.codemonkey.org.uk


2007-08-08 17:06:09

by Joel Schopp

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

> +# check for pointless casting of kmalloc return
> + if ($rawline =~ /\*\)[ ]k[czm]alloc/) {

It looks to me like this will catch

foo = (char *) kmalloc(512);

but not

for = (char* )kmalloc(512);

I haven't tried it but how about something like:

if($rawline =~/\(.*\)\s*k[czm]alloc/){

which if I got it right should match the typecast with any combination of spacing.

> + WARN("No need to cast return value.\n");

Could the warning be more descriptive? This describes what, but it should also describe
why; after all if somebody made this error they may not know they why.

2007-08-08 18:01:33

by Dave Jones

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

On Wed, Aug 08, 2007 at 12:05:13PM -0500, jschopp wrote:
> > +# check for pointless casting of kmalloc return
> > + if ($rawline =~ /\*\)[ ]k[czm]alloc/) {
>
> It looks to me like this will catch
>
> foo = (char *) kmalloc(512);
>
> but not
>
> for = (char* )kmalloc(512);
>
> I haven't tried it but how about something like:
>
> if($rawline =~/\(.*\)\s*k[czm]alloc/){

Hmm, could even just drop the check for the '*'
)[ ]k.. should be good enough.

> which if I got it right should match the typecast with any combination of spacing.
>
> > + WARN("No need to cast return value.\n");
>
> Could the warning be more descriptive? This describes what, but it should also describe
> why; after all if somebody made this error they may not know they why.

I'm open to suggestions..

Dave

--
http://www.codemonkey.org.uk

2007-08-08 18:11:08

by Cal Peake

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

On Wed, 8 Aug 2007, Dave Jones wrote:

> > I haven't tried it but how about something like:
> >
> > if($rawline =~/\(.*\)\s*k[czm]alloc/){
>
> Hmm, could even just drop the check for the '*'
> )[ ]k.. should be good enough.

There is none, both the '*'s are qualifiers :)

> > which if I got it right should match the typecast with any combination of spacing.
> >
> > > + WARN("No need to cast return value.\n");
> >
> > Could the warning be more descriptive? This describes what, but it should also describe
> > why; after all if somebody made this error they may not know they why.
>
> I'm open to suggestions..

Point to or quote from: http://c-faq.com/malloc/mallocnocast.html ?

--
Cal Peake

2007-08-08 18:28:07

by Joel Schopp

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

> > > + WARN("No need to cast return value.\n");
> >
> > Could the warning be more descriptive? This describes what, but it should also describe
> > why; after all if somebody made this error they may not know they why.
>
> I'm open to suggestions..

How about

WARN("No need to cast return value, it is unnecessary and can hide real bugs. See
http://c-faq.com/malloc/mallocnocast.html for more details\n");

2007-08-08 18:40:44

by Nish Aravamudan

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

On 8/8/07, jschopp <[email protected]> wrote:
> > > > + WARN("No need to cast return value.\n");
> > >
> > > Could the warning be more descriptive? This describes what, but it should also describe
> > > why; after all if somebody made this error they may not know they why.
> >
> > I'm open to suggestions..
>
> How about
>
> WARN("No need to cast return value, it is unnecessary and can hide real bugs. See
> http://c-faq.com/malloc/mallocnocast.html for more details\n");

That's a long line. Can it be split into two? Otherwise seems sane.

Thanks,
Nish

2007-08-13 13:50:57

by Andy Whitcroft

[permalink] [raw]
Subject: Re: Make checkpatch warn about pointless casting of kalloc returns.

jschopp wrote:
>> +# check for pointless casting of kmalloc return
>> + if ($rawline =~ /\*\)[ ]k[czm]alloc/) {
>
> It looks to me like this will catch
>
> foo = (char *) kmalloc(512);
>
> but not
>
> for = (char* )kmalloc(512);
>
> I haven't tried it but how about something like:
>
> if($rawline =~/\(.*\)\s*k[czm]alloc/){
>
> which if I got it right should match the typecast with any combination
> of spacing.
>
>> + WARN("No need to cast return value.\n");
>
> Could the warning be more descriptive? This describes what, but it
> should also describe why; after all if somebody made this error they may
> not know they why.

Yes there are a few problems with the match, plus it needs to be on the
processed line to avoid false matches in strings (however unlikely). I
ended up with the following:

if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
WARN("unnecessary cast may hide bugs, see
http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
}

Which just fits on the line :).

Thanks for the patch. Will be in 0.10 coming to an -mm near you soon.

-apw