Re: [PATCH] media, tlg2300: Fix memory leak in alloc_bulk_urbs_generic()

From: Huang Shijie
Date: Mon Jan 03 2011 - 10:09:09 EST


Hi Jesper:
Thanks for your patch.


2011/1/3 Jesper Juhl <jj@xxxxxxxxxxxxx>:
> Hi,
>
> While reading
> drivers/media/video/tlg2300/pd-video.c::alloc_bulk_urbs_generic() I
> noticed that
>
> Â- We don't free the memory allocated to 'urb' if the call to
> Â usb_alloc_coherent() fails.
Yes.
thanks.

> Â- If the 'num' argument to the function is ever <= 0 we'll return an
> Â uninitialized variable 'i' to the caller.

Do not worry about this. All the 'num' are macros which is greater
then zero now.

>
> The following patch addresses both of the above by a) calling
> usb_free_urb() when usb_alloc_coherent() fails and by explicitly
> initializing 'i' to zero.
> I also moved the variables 'mem' and 'urb' inside the for loop. This does
> not actually make any difference, it just seemed more correct to me to let
> variables exist only in the innermost scope they are used.
>

Acked-by: Huang Shijie <shijie8@xxxxxxxxx>

>
> Signed-off-by: Jesper Juhl <jj@xxxxxxxxxxxxx>
> ---
> Âpd-video.c | Â 13 +++++++------
> Â1 file changed, 7 insertions(+), 6 deletions(-)
>
> Âcompile tested only.
>
> diff --git a/drivers/media/video/tlg2300/pd-video.c b/drivers/media/video/tlg2300/pd-video.c
> index a1ffe18..df33a1d 100644
> --- a/drivers/media/video/tlg2300/pd-video.c
> +++ b/drivers/media/video/tlg2300/pd-video.c
> @@ -512,19 +512,20 @@ int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
> Â Â Â Â Â Â Â Â Â Â Â Âint buf_size, gfp_t gfp_flags,
> Â Â Â Â Â Â Â Â Â Â Â Âusb_complete_t complete_fn, void *context)
> Â{
> - Â Â Â struct urb *urb;
> - Â Â Â void *mem;
> - Â Â Â int i;
> + Â Â Â int i = 0;
>
> - Â Â Â for (i = 0; i < num; i++) {
> - Â Â Â Â Â Â Â urb = usb_alloc_urb(0, gfp_flags);
> + Â Â Â for (; i < num; i++) {
> + Â Â Â Â Â Â Â void *mem;
> + Â Â Â Â Â Â Â struct urb *urb = usb_alloc_urb(0, gfp_flags);
> Â Â Â Â Â Â Â Âif (urb == NULL)
> Â Â Â Â Â Â Â Â Â Â Â Âreturn i;
>
> Â Â Â Â Â Â Â Âmem = usb_alloc_coherent(udev, buf_size, gfp_flags,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &urb->transfer_dma);
> - Â Â Â Â Â Â Â if (mem == NULL)
> + Â Â Â Â Â Â Â if (mem == NULL) {
> + Â Â Â Â Â Â Â Â Â Â Â usb_free_urb(urb);
> Â Â Â Â Â Â Â Â Â Â Â Âreturn i;
> + Â Â Â Â Â Â Â }
>
> Â Â Â Â Â Â Â Âusb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, ep_addr),
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âmem, buf_size, complete_fn, context);
>
>
>
> --
> Jesper Juhl <jj@xxxxxxxxxxxxx> Â Â Â Â Â Âhttp://www.chaosbits.net/
> Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
> Plain text mails only, please.
>
>
--
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/