[PATCH 1/2] usb: storage: fix missing calls to free()

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Jun 29 22:54:10 PDT 2023


On 29.06.23 21:52, Denis Orlov wrote:
> Memory allocated with xzalloc() was not actually being freed in a few
> functions, resulting in memory leaks.
> 
> Signed-off-by: Denis Orlov <denorl2009 at gmail.com>

Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>

> ---
>  drivers/usb/storage/usb.c | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
> index dda7131960..6cdcc348e4 100644
> --- a/drivers/usb/storage/usb.c
> +++ b/drivers/usb/storage/usb.c
> @@ -169,7 +169,7 @@ static int read_capacity_16(struct us_blk_dev *usb_blkdev)
>  
>  	if (ret < 0) {
>  		dev_warn(dev, "Read Capacity(16) failed\n");
> -		return ret;
> +		goto fail;
>  	}
>  
>  	/* Note this is logical, not physical sector size */
> @@ -181,13 +181,17 @@ static int read_capacity_16(struct us_blk_dev *usb_blkdev)
>  
>  	if ((data[12] & 1) == 1) {
>  		dev_warn(dev, "Protection unsupported\n");
> -		return -ENOTSUPP;
> +		ret = -ENOTSUPP;
> +		goto fail;
>  	}
>  
>  	usb_blkdev->blk.blockbits = SECTOR_SHIFT;
>  	usb_blkdev->blk.num_blocks = lba + 1;
>  
> -	return sector_size;
> +	ret = sector_size;
> +fail:
> +	free(data);
> +	return ret;
>  }
>  
>  static int read_capacity_10(struct us_blk_dev *usb_blkdev)
> @@ -208,7 +212,7 @@ static int read_capacity_10(struct us_blk_dev *usb_blkdev)
>  
>  	if (ret < 0) {
>  		dev_warn(dev, "Read Capacity(10) failed\n");
> -		return ret;
> +		goto fail;
>  	}
>  
>  	sector_size = be32_to_cpu(data[1]);
> @@ -223,7 +227,10 @@ static int read_capacity_10(struct us_blk_dev *usb_blkdev)
>  	usb_blkdev->blk.num_blocks = lba + 1;
>  	usb_blkdev->blk.blockbits = SECTOR_SHIFT;
>  
> -	return SECTOR_SIZE;
> +	ret = SECTOR_SIZE;
> +fail:
> +	free(data);
> +	return ret;
>  }
>  
>  static int usb_stor_io_16(struct us_blk_dev *usb_blkdev, u8 opcode,

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list