Skip to contentSkip to conversation

qdev: Fix memory leak

PATCH Superseded dunrong huang 4 participants 5 messages May 3, 2012 – May 18, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qdev: Fix memory leak

The str allocated in visit_type_str was not freed

---
 hw/qdev-properties.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
hw/qdev-properties.c +6 -2
index 98dd06a..8088699 100644
@@ -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)
Signature · 1 line
1.7.8.4
Tested-by on v1 1/1
#1 lore raw

Hello,

Am 03.05.2012 10:34, schrieb dunrong huang:

Quoted 12 lines

The str allocated in visit_type_str was not freed

---
hw/qdev-properties.c | 8 ++++++--

1 files changed, 6 insertions(+), 2 deletions(-)
hw/qdev-properties.c +0 -0
index 98dd06a..8088699 100644

hw/qdev-properties.c static void set_mac(Object *obj, Visitor *v, void · line 730

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

Is this change really needed?

Quoted 18 lines
hw/qdev-properties.c +1 -0
 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 = {

hw/qdev-properties.c static void set_pci_devfn(Object *obj, Visitor *v, · line 831

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

As far as I could see, str does not need an initial value, so neither the old nor the new code is optimal (both versions do work).

Quoted 16 lines
hw/qdev-properties.c +1 -0
 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);
}

Quoted 1 line

static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)

Maybe some expert (Michael Roth?) can comment on the correct usage of visit_type_str.

Is the initial value for the 2nd argument really needed? The current QEMU code sometimes uses initialization,
but not always (see the code above, for example), so it is confusing.

Otherwise the patch is ok. It fixes memory leaks, therefore it should be added to QEMU 1.1.

Regards,

Stefan Weil

#2 lore raw
Quoted 75 lines · Stefan Weil

Hello,

Am 03.05.2012 10:34, schrieb dunrong huang:

Quoted 12 lines

The str allocated in visit_type_str was not freed

---
hw/qdev-properties.c | 8 ++++++--

1 files changed, 6 insertions(+), 2 deletions(-)
hw/qdev-properties.c +0 -0
index 98dd06a..8088699 100644

hw/qdev-properties.c static void set_mac(Object *obj, Visitor *v, · line 730

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

Is this change really needed?

Quoted 18 lines
hw/qdev-properties.c +1 -0
 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 = {

hw/qdev-properties.c static void set_pci_devfn(Object *obj, Visitor · line 831

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

As far as I could see, str does not need an initial value, so neither the old nor the new code is optimal (both versions do work).

Quoted 16 lines
hw/qdev-properties.c +1 -0
 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);
}

Quoted 2 lines

static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)

Maybe some expert (Michael Roth?) can comment on the correct usage of visit_type_str.

Quoted 3 lines

Is the initial value for the 2nd argument really needed? The current QEMU code sometimes uses initialization,
but not always (see the code above, for example), so it is confusing.

For an output visitor (native-to-<QMP/String/etc>), we assume we're getting a pointer to a valid string, and generally treat NULL as an indication to generate an empty string, so the initial value matters in that case. For that situation the changes would be warranted.

But for an input visitor (<QMP/String/etc>-to-native) like we're using in the setters this patch modifies, the initial value gets clobbered with a pointer to whatever the visitor allocates, so initializing the pointers isn't neccessary.

Quoted 10 lines

Otherwise the patch is ok. It fixes memory leaks, therefore it should be added to QEMU 1.1.

Quoted 1 line

Regards,

Stefan Weil

#3 lore raw

Thanks for your reply.

As you say, for an input visitor we dont need to initialize the pointer. "visit_type_str" in set_mac function and set_pci_devfn function is a input visitor, it points to
"qmp_input_type_str", if qmp_input_type_str failed, it will alloc a Error struct and return.

So, i think it doesnt matter whether initializing it or not in this situation.
But the str must be freed to avoid memory leak

2012/5/15 Michael Roth <[email protected]>

Quoted 104 lines · Stefan Weil
Quoted 75 lines

Hello,

Am 03.05.2012 10:34, schrieb dunrong huang:

Quoted 12 lines

The str allocated in visit_type_str was not freed

---
hw/qdev-properties.c | 8 ++++++--

1 files changed, 6 insertions(+), 2 deletions(-)
hw/qdev-properties.c +0 -0
index 98dd06a..8088699 100644

hw/qdev-properties.c static void set_mac(Object *obj, Visitor *v, · line 730

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

Is this change really needed?

Quoted 18 lines
hw/qdev-properties.c +1 -0
 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 = {

hw/qdev-properties.c static void set_pci_devfn(Object *obj, Visitor · line 831

Quoted 6 lines
hw/qdev-properties.c +1 -1
@@ -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;

As far as I could see, str does not need an initial value, so neither the old nor the new code is optimal (both versions do work).

Quoted 16 lines
hw/qdev-properties.c +1 -0
 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);
}

Quoted 2 lines

static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)

Maybe some expert (Michael Roth?) can comment on the correct usage of visit_type_str.

Quoted 3 lines

Is the initial value for the 2nd argument really needed? The current QEMU code sometimes uses initialization,
but not always (see the code above, for example), so it is confusing.

For an output visitor (native-to-<QMP/String/etc>), we assume we're getting a pointer to a valid string, and generally treat NULL as an indication
to generate an empty string, so the initial value matters in that case. For that situation the changes would be warranted.

But for an input visitor (<QMP/String/etc>-to-native) like we're using in the
setters this patch modifies, the initial value gets clobbered with a pointer
to whatever the visitor allocates, so initializing the pointers isn't neccessary.

Quoted 10 lines

Otherwise the patch is ok. It fixes memory leaks, therefore it should be added to QEMU 1.1.

Quoted 1 line

Regards,

Stefan Weil

Signature · 4 lines
linuxer and emacser and pythoner living in beijing
blog: http://mathslinux.org
twitter: https://twitter.com/mathslinux
google+: https://plus.google.com/118129852578326338750
#4 lore raw
Quoted 11 lines · dunrong huang

Thanks for your reply.

As you say, for an input visitor we dont need to initialize the pointer. "visit_type_str" in set_mac function and set_pci_devfn function is a input visitor, it points to
"qmp_input_type_str", if qmp_input_type_str failed, it will alloc a Error struct and return.

So, i think it doesnt matter whether initializing it or not in this situation.
But the str must be freed to avoid memory leak

Want to send a fixed "[PATCH 1.1 v2]" that can be merged for QEMU 1.1? Please go directly through Anthony Liguori or another qemu.git committer instead of qemu-trivial for QEMU 1.1 fixes.

Stefan