diff mbox

scsi: fix memory leak

Message ID 1333720125-32485-1-git-send-email-pbonzini@redhat.com
State New
Headers show

Commit Message

Paolo Bonzini April 6, 2012, 1:48 p.m. UTC
scsibus_get_dev_path is leaking id if it is not NULL.  Fix it.

Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/scsi-bus.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Stefan Hajnoczi April 10, 2012, 3:39 p.m. UTC | #1
On Fri, Apr 6, 2012 at 2:48 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> scsibus_get_dev_path is leaking id if it is not NULL.  Fix it.
>
> Reported-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  hw/scsi-bus.c |    7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
> index 8e76c5d..216b51f 100644
> --- a/hw/scsi-bus.c
> +++ b/hw/scsi-bus.c
> @@ -1430,15 +1430,18 @@ static char *scsibus_get_dev_path(DeviceState *dev)
>     SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
>     DeviceState *hba = dev->parent_bus->parent;
>     char *id = NULL;
> +    char *path;
>
>     if (hba && hba->parent_bus && hba->parent_bus->info->get_dev_path) {
>         id = hba->parent_bus->info->get_dev_path(hba);
>     }
>     if (id) {
> -        return g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
> +        path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
>     } else {
> -        return g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
> +        path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
>     }
> +    free(id);

Should be g_free(id).

Stefan
diff mbox

Patch

diff --git a/hw/scsi-bus.c b/hw/scsi-bus.c
index 8e76c5d..216b51f 100644
--- a/hw/scsi-bus.c
+++ b/hw/scsi-bus.c
@@ -1430,15 +1430,18 @@  static char *scsibus_get_dev_path(DeviceState *dev)
     SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
     DeviceState *hba = dev->parent_bus->parent;
     char *id = NULL;
+    char *path;
 
     if (hba && hba->parent_bus && hba->parent_bus->info->get_dev_path) {
         id = hba->parent_bus->info->get_dev_path(hba);
     }
     if (id) {
-        return g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
+        path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
     } else {
-        return g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
+        path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
     }
+    free(id);
+    return path;
 }
 
 static char *scsibus_get_fw_dev_path(DeviceState *dev)