Browse Source

qdev: Change PropertyInfo method print() to return malloc'ed string

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>
pull/307/head
Markus Armbruster 10 months ago
committed by Paolo Bonzini
parent
commit
6763d7be6b
  1. 7
      hw/core/qdev-properties-system.c
  2. 9
      hw/core/qdev-properties.c
  3. 2
      include/hw/qdev-properties.h

7
hw/core/qdev-properties-system.c

@ -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);
}
}

9
hw/core/qdev-properties.c

@ -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);
}
/**

2
include/hw/qdev-properties.h

@ -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);

Loading…
Cancel
Save