This simplifies the global validation code so all its logic is contained in a single function, and fixes the following:
$ qemu-system-x86_64 -global container.xxx=y hw/core/qdev-properties-system.c:450:qdev_add_one_global: Object 0x7f8d72a03d70 is not an instance of type device Aborted (core dumped) Signed-off-by: Eduardo Habkost <ehabk...@redhat.com> --- hw/core/qdev-properties-system.c | 16 ---------------- hw/core/qdev-properties.c | 25 +++++++++++++++++++------ include/hw/qdev-core.h | 5 ++--- tests/test-qdev-global-props.c | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index de433b2..404cf18 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -439,27 +439,11 @@ PropertyInfo qdev_prop_iothread = { static int qdev_add_one_global(QemuOpts *opts, void *opaque) { GlobalProperty *g; - ObjectClass *oc; g = g_malloc0(sizeof(*g)); g->driver = qemu_opt_get(opts, "driver"); g->property = qemu_opt_get(opts, "property"); g->value = qemu_opt_get(opts, "value"); - oc = object_class_by_name(g->driver); - if (oc) { - DeviceClass *dc = DEVICE_CLASS(oc); - - if (dc->hotpluggable) { - /* If hotpluggable then skip not_used checking. */ - g->not_used = false; - } else { - /* Maybe a typo. */ - g->not_used = true; - } - } else { - /* Maybe a typo. */ - g->not_used = true; - } qdev_prop_register_global(g); return 0; } diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 3d12560..c5a8fa0 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -961,13 +961,26 @@ int qdev_prop_check_global(void) int ret = 0; QTAILQ_FOREACH(prop, &global_props, next) { - if (!prop->not_used) { + ObjectClass *oc; + DeviceClass *dc; + if (prop->used) { + continue; + } + oc = object_class_by_name(prop->driver); + oc = object_class_dynamic_cast(oc, TYPE_DEVICE); + if (!oc) { + error_report("Warning: global %s.%s has invalid class name", + prop->driver, prop->property); + ret = 1; + continue; + } + dc = DEVICE_CLASS(oc); + if (!dc->hotpluggable && !prop->used) { + error_report("Warning: global %s.%s=%s\" not used", + prop->driver, prop->property, prop->value); + ret = 1; continue; } - ret = 1; - error_report("Warning: \"-global %s.%s=%s\" not used", - prop->driver, prop->property, prop->value); - } return ret; } @@ -983,7 +996,7 @@ void qdev_prop_set_globals_for_type(DeviceState *dev, const char *typename, if (strcmp(typename, prop->driver) != 0) { continue; } - prop->not_used = false; + prop->used = true; object_property_parse(OBJECT(dev), prop->value, prop->property, &err); if (err != NULL) { error_propagate(errp, err); diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 9221cfc..71ec282 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -241,8 +241,7 @@ struct PropertyInfo { /** * GlobalProperty: - * @not_used: Track use of a global property. Defaults to false in all C99 - * struct initializations. + * @used: Set to true if global property was used. * * This prevents reports of .compat_props when they are not used. */ @@ -250,7 +249,7 @@ typedef struct GlobalProperty { const char *driver; const char *property; const char *value; - bool not_used; + bool used; QTAILQ_ENTRY(GlobalProperty) next; } GlobalProperty; diff --git a/tests/test-qdev-global-props.c b/tests/test-qdev-global-props.c index 2bef04c..2a23ff5 100644 --- a/tests/test-qdev-global-props.c +++ b/tests/test-qdev-global-props.c @@ -143,6 +143,13 @@ static const TypeInfo dynamic_prop_type = { .class_init = dynamic_class_init, }; +#define TYPE_NONDEVICE "nondevice-type" + +static const TypeInfo nondevice_type = { + .name = TYPE_NONDEVICE, + .parent = TYPE_OBJECT, +}; + /* Test setting of static property using global properties */ static void test_dynamic_globalprop(void) { @@ -150,7 +157,8 @@ static void test_dynamic_globalprop(void) static GlobalProperty props[] = { { TYPE_DYNAMIC_PROPS, "prop1", "101" }, { TYPE_DYNAMIC_PROPS, "prop2", "102" }, - { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true }, + { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103" }, + { TYPE_NONDEVICE, "prop4", "104" }, {} }; int all_used; @@ -164,6 +172,10 @@ static void test_dynamic_globalprop(void) g_assert_cmpuint(mt->prop2, ==, 102); all_used = qdev_prop_check_global(); g_assert_cmpuint(all_used, ==, 1); + g_assert(props[0].used); + g_assert(props[1].used); + g_assert(!props[2].used); + g_assert(!props[3].used); } int main(int argc, char **argv) @@ -173,6 +185,7 @@ int main(int argc, char **argv) module_call_init(MODULE_INIT_QOM); type_register_static(&static_prop_type); type_register_static(&dynamic_prop_type); + type_register_static(&nondevice_type); g_test_add_func("/qdev/properties/static/default", test_static_prop); g_test_add_func("/qdev/properties/static/global", test_static_globalprop); -- 1.9.0