]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
qdev: Change PropertyInfo method print() to return malloc'ed string
authorMarkus Armbruster <armbru@redhat.com>
Wed, 22 Oct 2025 10:14:18 +0000 (12:14 +0200)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 28 Oct 2025 13:50:07 +0000 (14:50 +0100)
Simpler (more so after the next commit), and no risk of truncation
because the caller's buffer is too small.  Performance doesn't matter;
the method is only used for "info qdev".

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Link: https://lore.kernel.org/r/20251022101420.36059-2-armbru@redhat.com
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
hw/core/qdev-properties-system.c
hw/core/qdev-properties.c
include/hw/qdev-properties.h

index c15371f8cd163278e5101f384dbe6bd5ff879589..13cc91680b17b7dc1d20d265f92ff965e1530fce 100644 (file)
@@ -865,15 +865,14 @@ out:
     visit_end_alternate(v, (void **) &alt);
 }
 
-static int print_pci_devfn(Object *obj, const Property *prop, char *dest,
-                           size_t len)
+static char *print_pci_devfn(Object *obj, const Property *prop)
 {
     int32_t *ptr = object_field_prop_ptr(obj, prop);
 
     if (*ptr == -1) {
-        return snprintf(dest, len, "<unset>");
+        return g_strdup("<unset>");
     } else {
-        return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
+        return g_strdup_printf("%02x.%x", *ptr >> 3, *ptr & 7);
     }
 }
 
index b7e8a89ba5cd0a8cd27f2480b396cb808343b5ea..422a486969c8a21884acba7fd5c570f30754cbac 100644 (file)
@@ -1117,12 +1117,11 @@ static void qdev_get_legacy_property(Object *obj, Visitor *v,
                                      Error **errp)
 {
     const Property *prop = opaque;
+    char *s;
 
-    char buffer[1024];
-    char *ptr = buffer;
-
-    prop->info->print(obj, prop, buffer, sizeof(buffer));
-    visit_type_str(v, name, &ptr, errp);
+    s = prop->info->print(obj, prop);
+    visit_type_str(v, name, &s, errp);
+    g_free(s);
 }
 
 /**
index 0197aa4995e652b1e62f44ea8b89d6c9fa697591..60b81330097dc18294e74b0c6772e05832a1fe57 100644 (file)
@@ -34,7 +34,7 @@ struct PropertyInfo {
     const char *description;
     const QEnumLookup *enum_table;
     bool realized_set_allowed; /* allow setting property on realized device */
-    int (*print)(Object *obj, const Property *prop, char *dest, size_t len);
+    char *(*print)(Object *obj, const Property *prop);
     void (*set_default_value)(ObjectProperty *op, const Property *prop);
     ObjectProperty *(*create)(ObjectClass *oc, const char *name,
                               const Property *prop);