Skip to contentSkip to conversation

qom: strdup() target_name on object_property_add_alias()

PATCH Applied Eduardo Habkost 4 participants 8 messages Apr 9, 2015 – May 18, 2015

lore
Share & tools
lore
#0 lore raw

cover qom: strdup() target_name on object_property_add_alias()

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/2 qom: strdup() target property name on object_property_add_alias()

With this, object_property_add_alias() callers can safely free the target property name, like what already happens with the 'name' argument to all object_property_add*() functions.

---
 qom/object.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
qom/object.c +3 -2
index b8dff43..550dd48 100644
@@ -1689,7 +1689,7 @@ void object_property_add_uint64_ptr(Object *obj, const char *name,
 
 typedef struct {
     Object *target_obj;
-    const char *target_name;
+    char *target_name;
 } AliasProperty;
 
 static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
@@ -1720,6 +1720,7 @@ static void property_release_alias(Object *obj, const char *name, void *opaque)
 {
     AliasProperty *prop = opaque;
 
+    g_free(prop->target_name);
     g_free(prop);
 }
 
@@ -1747,7 +1748,7 @@ void object_property_add_alias(Object *obj, const char *name,
 
     prop = g_malloc(sizeof(*prop));
     prop->target_obj = target_obj;
-    prop->target_name = target_name;
+    prop->target_name = g_strdup(target_name);
 
     op = object_property_add(obj, name, prop_type,
                              property_get_alias,
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/2 qdev: Free property names after registering gpio aliases

In reply to Eduardo Habkost

Now that object_property_add_alias() strdup()s target_name, we can free the property names in qdev_pass_gpios().

---
 hw/core/qdev.c | 2 ++
 1 file changed, 2 insertions(+)
hw/core/qdev.c +2 -0
index 6e6a65d..6dca6cb 100644
@@ -563,6 +563,7 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
         object_property_add_alias(OBJECT(container), propname,
                                   OBJECT(dev), propname,
                                   &error_abort);
+        g_free(propname);
     }
     for (i = 0; i < ngl->num_out; i++) {
         const char *nm = ngl->name ? ngl->name : "unnamed-gpio-out";
@@ -571,6 +572,7 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,
         object_property_add_alias(OBJECT(container), propname,
                                   OBJECT(dev), propname,
                                   &error_abort);
+        g_free(propname);
     }
     QLIST_REMOVE(ngl, node);
     QLIST_INSERT_HEAD(&container->gpios, ngl, node);
Signature · 1 line
2.1.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Eduardo Habkost
Quoted 11 lines · Eduardo Habkost

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)

Good idea!

#4 lore raw

Am 09.04.2015 um 22:22 schrieb Paolo Bonzini:

Quoted 8 lines · Eduardo Habkost
Quoted 7 lines

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

Quoted 8 lines
Quoted 4 lines
 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)

Good idea!

Looks good to me in general, I'm just surprised that there are not more cleanups resulting out of this? At least my pc87312 series will need changes then. Will take a deeper look another day.

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#5 lore raw

Am 10.04.2015 um 00:48 schrieb Andreas Färber:

Quoted 14 lines

Am 09.04.2015 um 22:22 schrieb Paolo Bonzini:

Quoted 13 lines · Eduardo Habkost
Quoted 11 lines

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 3 lines

Good idea!

Looks good to me in general, I'm just surprised that there are not more cleanups resulting out of this? At least my pc87312 series will need changes then. Will take a deeper look another day.

Thanks, applied to qom-next:
https://github.com/afaerber/qemu-cpu/commits/qom-next

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#6 lore raw
Quoted 17 lines · Andreas Färber

Am 10.04.2015 um 00:48 schrieb Andreas Färber:

Quoted 16 lines

Am 09.04.2015 um 22:22 schrieb Paolo Bonzini:

Quoted 15 lines · Eduardo Habkost
Quoted 11 lines

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)

Good idea!

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Looks good to me in general, I'm just surprised that there are not more cleanups resulting out of this? At least my pc87312 series will need changes then. Will take a deeper look another day.

Thanks, applied to qom-next:
https://github.com/afaerber/qemu-cpu/commits/qom-next

Thanks. Is it expected to appear in a pull request soon? This is needed for the X86CPU properties patch that's on the x86 queue.

Signature · 1 line
Eduardo
Reviewed-by on the v1 series
#7 lore raw
In reply to Eduardo Habkost
Quoted 14 lines · Eduardo Habkost

This helps us avoid memory leaks when using object_property_add_alias(), as it is not practical for callers to save target_name to free it later.

Eduardo Habkost (2):
qom: strdup() target property name on object_property_add_alias()
qdev: Free property names after registering gpio aliases

 hw/core/qdev.c | 2 ++
 qom/object.c   | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.1.0