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 <><
_______________________________________________
Devel mailing list
[email protected]
http://mailman.laptop.org/mailman/listinfo/devel

Reply via email to