Re: [PATCH] scripts/checkpatch.pl: Add WARN on sizeof(&

From: AmÃrico Wang
Date: Mon Oct 05 2009 - 04:01:37 EST


On Sun, Oct 4, 2009 at 9:12 AM, Joe Perches <joe@xxxxxxxxxxx> wrote:
> On Sat, 2009-10-03 at 17:38 +0200, Jan Ceuleers wrote:
>> David Miller wrote:
>> > Any time you see "&" in a sizeof() expression, it's almost
>> > certainly a bug. ÂSomething for the folks with automated
>> > tools to look for if they haven't already :-)
>>
>> Your remark prompted me to find four more instances of such bugs
>> (none of which in the networking bits). I have submitted patches.
>
> Perhaps a checkpatch test.

A quick grep shows we have more:

grep -e '\bsizeof[:space:]*([:space:]*\&.*)' -nr ./
./drivers/media/video/em28xx/em28xx-cards.c:2237:
memset(&dev->info, 0, sizeof(&dev->info));
./drivers/media/video/saa7164/saa7164-cmd.c:350:
memset(&response_t, 0, sizeof(&response_t));
./drivers/media/dvb/dvb-usb/ce6230.c:108: memset(&req, 0, sizeof(&req));
./drivers/video/backlight/lcd.c:59: memset(&ld->fb_notif, 0,
sizeof(&ld->fb_notif));

>
> ---
>
> sizeof(&foo) is frequently an error. ÂWarn on its use.
>
> Signed-off-by: Joe Perches <joe@xxxxxxxxxxx>


This patch looks fine for me,

Acked-by: WANG Cong <xiyou.wangcong@xxxxxxxxx>


>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 87bbb8b..596dd72 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -2534,6 +2534,11 @@ sub process {
> Â Â Â Â Â Â Â Â Â Â Â ÂWARN("plain inline is preferred over $1\n" . $herecurr);
> Â Â Â Â Â Â Â Â}
>
> +# check for sizeof(&)
> + Â Â Â Â Â Â Â if ($line =~ /\bsizeof\s*\(\s*\&/) {
> + Â Â Â Â Â Â Â Â Â Â Â WARN("sizeof(& should be avoided\n" . $herecurr);
> + Â Â Â Â Â Â Â }
> +
> Â# check for new externs in .c files.
> Â Â Â Â Â Â Â Âif ($realfile =~ /\.c$/ && defined $stat &&
> Â Â Â Â Â Â Â Â Â Â$stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/