Re: [PATCH next V2] drivers: Use NULL not zero pointer assignments

Ryan Mallon
2012-01-27T22:27:55Z

On 27/01/12 18:38, Joe Perches wrote:
> On Fri, 2012-01-27 at 17:20 +1100, Ryan Mallon wrote:
>> On 27/01/12 16:33, Joe Perches wrote:
>>> Using NULL pointer assignments is more kernel-style standard.
>>> Uncompiled, untested.
>>> Done via cocinelle script:
>>> @@
>>> type T;
>>> T *pointer;
>>> @@
>>> -pointer = 0
>>> +pointer = NULL
>> Hi Joe,
> Hi Ryan.
>
>> I think you can drop a lot of the initialisations completely rather than
>> convert them. I've pointed some out below. I'm just scanning through for
>> likely candidates and I didn't bother looking at the staging drivers, so
>> this list is not comprehensive.
> I'll try a more generic solution.
> Here's a possible cocci script that looks for declarations with
> initializations that are later overwritten.

I think you need to be careful with this. I don't know enough about Coccinelle, but in general control flow makes this a difficult problem. This are also the cases where a variable is assigned on all control paths before being used, but also has an (seemingly useless) initialiser because gcc occasionally cannot determine that the variable is always initialised (because in general it is undecidable) and so the initialisation is there to prevent a compiler warning.

There were also a couple of cases I pointed out where the assignment of zero to a pointer variable was actually masking some other bug, and so replacing the assignment with NULL, or removing the initialisation is not necessarily the correct fix.

A script might be useful for identifying potential cases, but I think it is worth looking at each one individually to determine what the correct fix is.

~Ryan

> $ cat multi_set.cocci
> @@
> type T;
> identifier x;
> expression y;
> expression z;
> @@
> -T x = y;
> +T x;
> ...
> x = z
> $
>
> This script has a defect because expression z may
> use identifier x.
>
> Anyway, if the script can be tweaked appropriately,
> it might be useful to remove these unnecessary
> initializations.
>
> cheers, Joe
>