Skip to contentSkip to conversation

Make checkpatch warn about pointless casting of kalloc returns.

PATCH Dave Jones 5 participants 7 messages Aug 8, 2007 – Aug 13, 2007 Checkpatch

lore
Share & tools
lore

Dave Jones

author
#0 lore raw

Make checkpatch warn about pointless casting of kalloc returns.

scripts/checkpatch.pl +5 -0
index 73751ab..c6cae6a 100755
@@ -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) {
Signature · 1 line
http://www.codemonkey.org.uk
#1 lore raw

scripts/checkpatch.pl sub process { · line 1056

Quoted 2 lines
scripts/checkpatch.pl +2 -0
+# 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.

scripts/checkpatch.pl sub process { · line 1057

Quoted 1 line
scripts/checkpatch.pl +1 -0
+			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.

author
#2 lore raw
Quoted 7 lines · jschopp

scripts/checkpatch.pl sub process { · line 1056

Quoted 2 lines
scripts/checkpatch.pl +2 -0
+# check for pointless casting of kmalloc return
+		if ($rawline =~ /\*\)[ ]k[czm]alloc/) {

It looks to me like this will catch

foo = (char *) kmalloc(512);

Quoted 7 lines

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.

Quoted 6 lines

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

scripts/checkpatch.pl sub process { · line 1057

Quoted 1 line
scripts/checkpatch.pl +1 -0
+			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

Signature · 1 line
http://www.codemonkey.org.uk
#3 lore raw
Quoted 6 lines · Dave Jones
Quoted 3 lines

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 :)

Quoted 8 lines
Quoted 6 lines

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

scripts/checkpatch.pl sub process { · line 1057

Quoted 1 line
scripts/checkpatch.pl +1 -0
+			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 ?

Signature · 1 line
Cal Peake
#4 lore raw
In reply to Dave Jones
Quoted 6 lines
Quoted 4 lines

scripts/checkpatch.pl sub process { · line 1057

Quoted 1 line
scripts/checkpatch.pl +1 -0
+			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");

#5 lore raw
Quoted 11 lines · jschopp
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+ 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

#6 lore raw
In reply to jschopp
Quoted 15 lines · jschopp

scripts/checkpatch.pl sub process { · line 1056

Quoted 2 lines
scripts/checkpatch.pl +2 -0
+# 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/){

Quoted 8 lines

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

Quoted 1 line

+ 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