Patchwork qdev: Fix memory leak

login
register
mail settings
Submitter dunrong huang
Date May 3, 2012, 7:02 a.m.
Message ID <1336028542-785-1-git-send-email-riegamaths@gmail.com>
Download mbox | patch
Permalink /patch/156770/
State New
Headers show

Comments

dunrong huang - May 3, 2012, 7:02 a.m.
The str allocated in visit_type_str was not freed

Signed-off-by: dunrong huang <riegamaths@gmail.com>
---
 hw/qdev-properties.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
dunrong huang - May 3, 2012, 8:33 a.m.
Sorry, i miss the "[PATCH]" in mail's title, i will resend it again.

2012/5/3 dunrong huang <riegamaths@gmail.com>

> The str allocated in visit_type_str was not freed
>
> Signed-off-by: dunrong huang <riegamaths@gmail.com>
> ---
>  hw/qdev-properties.c |    8 ++++++--
>  1 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
> index 98dd06a..8088699 100644
> --- a/hw/qdev-properties.c
> +++ b/hw/qdev-properties.c
> @@ -726,7 +726,7 @@ static void set_mac(Object *obj, Visitor *v, void
> *opaque,
>     MACAddr *mac = qdev_get_prop_ptr(dev, prop);
>     Error *local_err = NULL;
>     int i, pos;
> -    char *str, *p;
> +    char *str = NULL, *p;
>
>     if (dev->state != DEV_STATE_CREATED) {
>         error_set(errp, QERR_PERMISSION_DENIED);
> @@ -753,10 +753,12 @@ static void set_mac(Object *obj, Visitor *v, void
> *opaque,
>         }
>         mac->a[i] = strtol(str+pos, &p, 16);
>     }
> +    g_free(str);
>     return;
>
>  inval:
>     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
> +    g_free(str);
>  }
>
>  PropertyInfo qdev_prop_macaddr = {
> @@ -825,7 +827,7 @@ static void set_pci_devfn(Object *obj, Visitor *v,
> void *opaque,
>     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
>     unsigned int slot, fn, n;
>     Error *local_err = NULL;
> -    char *str = (char *)"";
> +    char *str = NULL;
>
>     if (dev->state != DEV_STATE_CREATED) {
>         error_set(errp, QERR_PERMISSION_DENIED);
> @@ -847,10 +849,12 @@ static void set_pci_devfn(Object *obj, Visitor *v,
> void *opaque,
>         goto invalid;
>     }
>     *ptr = slot << 3 | fn;
> +    g_free(str);
>     return;
>
>  invalid:
>     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
> +    g_free(str);
>  }
>
>  static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest,
> size_t len)
> --
> 1.7.8.4
>
>

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 98dd06a..8088699 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -726,7 +726,7 @@  static void set_mac(Object *obj, Visitor *v, void *opaque,
     MACAddr *mac = qdev_get_prop_ptr(dev, prop);
     Error *local_err = NULL;
     int i, pos;
-    char *str, *p;
+    char *str = NULL, *p;
 
     if (dev->state != DEV_STATE_CREATED) {
         error_set(errp, QERR_PERMISSION_DENIED);
@@ -753,10 +753,12 @@  static void set_mac(Object *obj, Visitor *v, void *opaque,
         }
         mac->a[i] = strtol(str+pos, &p, 16);
     }
+    g_free(str);
     return;
 
 inval:
     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
+    g_free(str);
 }
 
 PropertyInfo qdev_prop_macaddr = {
@@ -825,7 +827,7 @@  static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
     uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
     unsigned int slot, fn, n;
     Error *local_err = NULL;
-    char *str = (char *)"";
+    char *str = NULL;
 
     if (dev->state != DEV_STATE_CREATED) {
         error_set(errp, QERR_PERMISSION_DENIED);
@@ -847,10 +849,12 @@  static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
         goto invalid;
     }
     *ptr = slot << 3 | fn;
+    g_free(str);
     return;
 
 invalid:
     error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
+    g_free(str);
 }
 
 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)