All of lore.kernel.org
 help / color / mirror / Atom feed
* Make checkpatch warn about pointless casting of kalloc returns.
@ 2007-08-08  2:43 Dave Jones
  2007-08-08 17:05 ` jschopp
  0 siblings, 1 reply; 7+ messages in thread
From: Dave Jones @ 2007-08-08  2:43 UTC (permalink / raw)
  To: apw; +Cc: rdunlap, jschopp, Linux Kernel

Make checkpatch warn about pointless casting of kalloc returns.

Signed-off-by: Dave Jones <davej@redhat.com>

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08  2:43 Make checkpatch warn about pointless casting of kalloc returns Dave Jones
@ 2007-08-08 17:05 ` jschopp
  2007-08-08 18:00   ` Dave Jones
  2007-08-13 10:24   ` Andy Whitcroft
  0 siblings, 2 replies; 7+ messages in thread
From: jschopp @ 2007-08-08 17:05 UTC (permalink / raw)
  To: Dave Jones, apw, rdunlap, jschopp, Linux Kernel

> +# 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08 17:05 ` jschopp
@ 2007-08-08 18:00   ` Dave Jones
  2007-08-08 18:10     ` Cal Peake
  2007-08-08 18:27     ` jschopp
  2007-08-13 10:24   ` Andy Whitcroft
  1 sibling, 2 replies; 7+ messages in thread
From: Dave Jones @ 2007-08-08 18:00 UTC (permalink / raw)
  To: jschopp; +Cc: apw, rdunlap, Linux Kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08 18:00   ` Dave Jones
@ 2007-08-08 18:10     ` Cal Peake
  2007-08-08 18:27     ` jschopp
  1 sibling, 0 replies; 7+ messages in thread
From: Cal Peake @ 2007-08-08 18:10 UTC (permalink / raw)
  To: Dave Jones; +Cc: jschopp, apw, rdunlap, Linux Kernel

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08 18:00   ` Dave Jones
  2007-08-08 18:10     ` Cal Peake
@ 2007-08-08 18:27     ` jschopp
  2007-08-08 18:40       ` Nish Aravamudan
  1 sibling, 1 reply; 7+ messages in thread
From: jschopp @ 2007-08-08 18:27 UTC (permalink / raw)
  To: Dave Jones, jschopp, apw, rdunlap, Linux Kernel

>  > > +			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");

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08 18:27     ` jschopp
@ 2007-08-08 18:40       ` Nish Aravamudan
  0 siblings, 0 replies; 7+ messages in thread
From: Nish Aravamudan @ 2007-08-08 18:40 UTC (permalink / raw)
  To: jschopp; +Cc: Dave Jones, apw, rdunlap, Linux Kernel

On 8/8/07, jschopp <jschopp@austin.ibm.com> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Make checkpatch warn about pointless casting of kalloc returns.
  2007-08-08 17:05 ` jschopp
  2007-08-08 18:00   ` Dave Jones
@ 2007-08-13 10:24   ` Andy Whitcroft
  1 sibling, 0 replies; 7+ messages in thread
From: Andy Whitcroft @ 2007-08-13 10:24 UTC (permalink / raw)
  To: Dave Jones; +Cc: jschopp, rdunlap, Linux Kernel

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-08-13 13:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-08  2:43 Make checkpatch warn about pointless casting of kalloc returns Dave Jones
2007-08-08 17:05 ` jschopp
2007-08-08 18:00   ` Dave Jones
2007-08-08 18:10     ` Cal Peake
2007-08-08 18:27     ` jschopp
2007-08-08 18:40       ` Nish Aravamudan
2007-08-13 10:24   ` Andy Whitcroft

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.