Detect ambiguous matches right away, without going through all interfaces first.
Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> --- qom/object.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/qom/object.c b/qom/object.c index f82f12c..c69e6a3 100644 --- a/qom/object.c +++ b/qom/object.c @@ -455,7 +455,6 @@ Object *object_dynamic_cast_assert(Object *obj, const char *typename, ObjectClass *object_class_dynamic_cast(ObjectClass *class, const char *typename) { - ObjectClass *ret = NULL; TypeImpl *target_type; if (!class) { @@ -475,27 +474,26 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *class, if (class->interfaces && type_is_ancestor(target_type, type_interface)) { - int found = 0; + ObjectClass *ret = NULL; GSList *i; for (i = class->interfaces; i; i = i->next) { ObjectClass *target_class = i->data; if (type_is_ancestor(target_class->type, target_type)) { + /* If the match is ambiguous, don't allow a cast */ + if (ret) { + ret = NULL; + } ret = target_class; - found++; } } - - /* The match was ambiguous, don't allow a cast */ - if (found > 1) { - ret = NULL; - } + return ret; } else if (type_is_ancestor(class->type, target_type)) { - ret = class; + return class; + } else { + return NULL; } - - return ret; } ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, -- 1.8.1.4