[PATCH] qdev: Use g_autofree for visited strings

Fabiano Rosas posted 1 patch 6 days, 7 hours ago
hw/core/qdev-properties-system.c | 34 +++++++++++---------------------
1 file changed, 11 insertions(+), 23 deletions(-)
[PATCH] qdev: Use g_autofree for visited strings
Posted by Fabiano Rosas 6 days, 7 hours ago
Use g_autofree for visited strings. This plugs leaks for set_chr() and
set_drive_helper(), the latter verifiable in qos-test. For the rest of
the changed functions, it keeps the code uniform.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 hw/core/qdev-properties-system.c | 34 +++++++++++---------------------
 1 file changed, 11 insertions(+), 23 deletions(-)

diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c
index 4d605c20561..271d8049797 100644
--- a/hw/core/qdev-properties-system.c
+++ b/hw/core/qdev-properties-system.c
@@ -120,7 +120,7 @@ static void set_drive_helper(Object *obj, Visitor *v, const char *name,
     DeviceState *dev = DEVICE(obj);
     const Property *prop = opaque;
     void **ptr = object_field_prop_ptr(obj, prop);
-    char *str;
+    g_autofree char *str = NULL;
     BlockBackend *blk;
     bool blk_created = false;
     int ret;
@@ -155,7 +155,6 @@ static void set_drive_helper(Object *obj, Visitor *v, const char *name,
     }
 
     if (!*str) {
-        g_free(str);
         *ptr = NULL;
         return;
     }
@@ -209,8 +208,6 @@ fail:
         /* If we need to keep a reference, blk_attach_dev() took it */
         blk_unref(blk);
     }
-
-    g_free(str);
 }
 
 static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
@@ -278,7 +275,7 @@ static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
     const Property *prop = opaque;
     CharFrontend *fe = object_field_prop_ptr(obj, prop);
     Chardev *s;
-    char *str;
+    g_autofree char *str = NULL;
 
     if (!visit_type_str(v, name, &str, errp)) {
         return;
@@ -293,7 +290,6 @@ static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
     }
 
     if (!*str) {
-        g_free(str);
         fe->chr = NULL;
         return;
     }
@@ -306,7 +302,6 @@ static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
         error_prepend(errp, "Property '%s.%s' can't take value '%s': ",
                       object_get_typename(obj), name, str);
     }
-    g_free(str);
 }
 
 static void release_chr(Object *obj, const char *name, void *opaque)
@@ -354,7 +349,7 @@ static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
     const Property *prop = opaque;
     MACAddr *mac = object_field_prop_ptr(obj, prop);
     int i, pos;
-    char *str;
+    g_autofree char *str = NULL;
     const char *p;
 
     if (!visit_type_str(v, name, &str, errp)) {
@@ -384,12 +379,10 @@ static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
         }
         mac->a[i] = val;
     }
-    g_free(str);
     return;
 
 inval:
     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
-    g_free(str);
 }
 
 const PropertyInfo qdev_prop_macaddr = {
@@ -430,7 +423,7 @@ static void set_netdev(Object *obj, Visitor *v, const char *name,
     NetClientState **ncs = peers_ptr->ncs;
     NetClientState *peers[MAX_QUEUE_NUM];
     int queues, err = 0, i = 0;
-    char *str;
+    g_autofree char *str = NULL;
 
     if (!visit_type_str(v, name, &str, errp)) {
         return;
@@ -478,7 +471,6 @@ static void set_netdev(Object *obj, Visitor *v, const char *name,
 
 out:
     error_set_from_qdev_prop_error(errp, err, obj, prop->name, str);
-    g_free(str);
 }
 
 const PropertyInfo qdev_prop_netdev = {
@@ -790,7 +782,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,
     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
     const char *endptr;
     uint64_t lob, upb;
-    char *str;
+    g_autofree char *str = NULL;
     int ret;
 
     if (!visit_type_str(v, name, &str, errp)) {
@@ -801,7 +793,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,
     if (ret) {
         error_setg(errp, "start address of '%s'"
                    " must be a hexadecimal integer", name);
-        goto out;
+        return;
     }
     if (*endptr != ':') {
         goto separator_error;
@@ -811,7 +803,7 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,
     if (ret) {
         error_setg(errp, "end address of '%s'"
                    " must be a hexadecimal integer", name);
-        goto out;
+        return;
     }
     if (*endptr != ':') {
         goto separator_error;
@@ -824,12 +816,10 @@ static void set_reserved_region(Object *obj, Visitor *v, const char *name,
         error_setg(errp, "type of '%s'"
                    " must be a non-negative decimal integer", name);
     }
-    goto out;
+    return;
 
 separator_error:
     error_setg(errp, "reserved region fields must be separated with ':'");
-out:
-    g_free(str);
 }
 
 const PropertyInfo qdev_prop_reserved_region = {
@@ -964,7 +954,8 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
 {
     const Property *prop = opaque;
     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
-    char *str, *p;
+    g_autofree char *str = NULL;
+    char *p;
     char *e;
     unsigned long val;
     unsigned long dom = 0, bus = 0;
@@ -1020,12 +1011,10 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
     addr->slot = slot;
     addr->function = func;
 
-    g_free(str);
     return;
 
 inval:
     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
-    g_free(str);
 }
 
 const PropertyInfo qdev_prop_pci_host_devaddr = {
@@ -1240,7 +1229,7 @@ static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
 {
     const Property *prop = opaque;
     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
-    char *str;
+    g_autofree char *str = NULL;
 
     if (!visit_type_str(v, name, &str, errp)) {
         return;
@@ -1251,7 +1240,6 @@ static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
     } else if (qemu_uuid_parse(str, uuid) < 0) {
         error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
     }
-    g_free(str);
 }
 
 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
-- 
2.53.0
Re: [PATCH] qdev: Use g_autofree for visited strings
Posted by Peter Maydell 5 days, 18 hours ago
On Mon, 16 Nov 2026 at 20:58, Fabiano Rosas <farosas@suse.de> wrote:
>
> Use g_autofree for visited strings. This plugs leaks for set_chr() and
> set_drive_helper(), the latter verifiable in qos-test. For the rest of
> the changed functions, it keeps the code uniform.

The set_drive_helper leak is already fixed in git by commit fc8bb17e476
(with the same changes you have for it in this patch).

For the others here

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM