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