Re: [PATCH] media: usb: go7007: Fix warning: passing freed memory 'fw'

From: Hans Verkuil
Date: Fri Aug 11 2023 - 05:58:31 EST


On 09/08/2023 03:00, Madhumitha Prabakaran wrote:
> Fix smatch warning - go7007_loader_probe() warn: passing freed memory 'fw'
>
> The 'fw' pointer is released using release_firmware(fw) and then being used
> again in another request_firmware() call without being reassigned to a new
> memory allocation. To resolve it, use separate variables for each request.

Hmm, I don't get this smatch warning. And it doesn't appear to be an actual
bug either, so that warning would be a false positive.

I wonder if you tested with an older smatch version?

Regards,

Hans

>
> Signed-off-by: Madhumitha Prabakaran <madhumithabiw@xxxxxxxxx>
> ---
> drivers/media/usb/go7007/go7007-loader.c | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/media/usb/go7007/go7007-loader.c b/drivers/media/usb/go7007/go7007-loader.c
> index 243aa0ad074c..5f5c425f4d45 100644
> --- a/drivers/media/usb/go7007/go7007-loader.c
> +++ b/drivers/media/usb/go7007/go7007-loader.c
> @@ -35,7 +35,7 @@ static int go7007_loader_probe(struct usb_interface *interface,
> const struct usb_device_id *id)
> {
> struct usb_device *usbdev;
> - const struct firmware *fw;
> + const struct firmware *fw_1, *fw_2;
> u16 vendor, product;
> const char *fw1, *fw2;
> int ret;
> @@ -67,13 +67,13 @@ static int go7007_loader_probe(struct usb_interface *interface,
>
> dev_info(&interface->dev, "loading firmware %s\n", fw1);
>
> - if (request_firmware(&fw, fw1, &usbdev->dev)) {
> + if (request_firmware(&fw_1, fw1, &usbdev->dev)) {
> dev_err(&interface->dev,
> "unable to load firmware from file \"%s\"\n", fw1);
> goto failed2;
> }
> - ret = cypress_load_firmware(usbdev, fw, CYPRESS_FX2);
> - release_firmware(fw);
> + ret = cypress_load_firmware(usbdev, fw_1, CYPRESS_FX2);
> + release_firmware(fw_1);
> if (0 != ret) {
> dev_err(&interface->dev, "loader download failed\n");
> goto failed2;
> @@ -82,13 +82,13 @@ static int go7007_loader_probe(struct usb_interface *interface,
> if (fw2 == NULL)
> return 0;
>
> - if (request_firmware(&fw, fw2, &usbdev->dev)) {
> + if (request_firmware(&fw_2, fw2, &usbdev->dev)) {
> dev_err(&interface->dev,
> "unable to load firmware from file \"%s\"\n", fw2);
> goto failed2;
> }
> - ret = cypress_load_firmware(usbdev, fw, CYPRESS_FX2);
> - release_firmware(fw);
> + ret = cypress_load_firmware(usbdev, fw_2, CYPRESS_FX2);
> + release_firmware(fw_2);
> if (0 != ret) {
> dev_err(&interface->dev, "firmware download failed\n");
> goto failed2;