[OLPC-devel] Announce: Compressed cache 'pre-alpha-001' release :)

Arnd Bergmann arnd at arndb.de
Thu Jul 27 03:55:31 EDT 2006


On Thursday 27 July 2006 09:29, Nitin Gupta wrote:
> some_func()
> {
>     void *a, *b;
>     a = alloc()
>     if (!a) goto out:
>     b = alloc();
>     ...
> out:
>     if (a) free(a)
>     if (b) free(b)
>     ...
> }
> 
> Here you (correctly) get 'possible uninitialized usage for b' warning. 
> That's why I initialize those local variables.

Well, I'd prefer to write this as 

some_func()
{
    void *a, *b;
    int ret;
    ret = -ENOMEM;
    a = alloc();
    if (!a)
	goto out_a;
    b = alloc();
    if (!b)
	goto out_b;
    ...
    ret = 0;
    free(b);    
out_b:
    free(a);
out_a:
    return ret;
}

Note:
 - statement after if() on following line
 - no variables initialized on declaration (ret could be)
 - only clean up what you created.
 - kfree(NULL); is ok

	Arnd <><



More information about the Devel mailing list