On Fri, 05 Sep 2014 10:39:28 +0200
Alexander Kurpiers <[email protected]> wrote:

> @@ -1707,9 +1709,14 @@ static int _rtlsdr_alloc_async_buffers(rtlsdr_dev_t 
> *dev)
>       if (!dev->xfer_buf) {
>               dev->xfer_buf = malloc(dev->xfer_buf_num *
>                                          sizeof(unsigned char *));
> +     if (dev->xfer_buf == NULL)
> +             return -1;
>  
> -             for(i = 0; i < dev->xfer_buf_num; ++i)
> +             for(i = 0; i < dev->xfer_buf_num; ++i){
>                       dev->xfer_buf[i] = malloc(dev->xfer_buf_len);
> +                     if (dev->xfer_buf[i] == NULL)
> +                             return -1;
> +             }
>       }

Please don't do stuff like that. And I'm not just talking about
the wrong indentation. If a function runs a bunch of allocs inside,
and one of them fails, it must unroll those that succeeded before
returning the error to the caller. Failure to do it is why you
had to add stuff like this:

> -     _rtlsdr_alloc_async_buffers(dev);
> +     /* Check the error code. Free return if errors */
> +     if(_rtlsdr_alloc_async_buffers(dev) < 0) {
> +             _rtlsdr_free_async_buffers(dev);
> +             return -1;
> +     }

Just say no to programming like this. The object is either initialized
or not initialized, and never half-way initialized.

-- Pete

Reply via email to