From patchwork Thu May 2 20:08:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Herv=C3=A9_Poussineau?= X-Patchwork-Id: 241072 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C77C42C00A9 for ; Fri, 3 May 2013 06:09:51 +1000 (EST) Received: from localhost ([::1]:45964 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UXzp7-0004QP-U1 for incoming@patchwork.ozlabs.org; Thu, 02 May 2013 16:09:49 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36713) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UXznj-0002lv-NS for qemu-devel@nongnu.org; Thu, 02 May 2013 16:08:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UXzni-0000r7-7T for qemu-devel@nongnu.org; Thu, 02 May 2013 16:08:23 -0400 Received: from smtp1-g21.free.fr ([2a01:e0c:1:1599::10]:44426) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UXznh-0000qi-LQ; Thu, 02 May 2013 16:08:22 -0400 Received: from localhost.localdomain (unknown [82.227.227.196]) by smtp1-g21.free.fr (Postfix) with ESMTP id 9B096940063; Thu, 2 May 2013 22:08:15 +0200 (CEST) From: =?UTF-8?q?Herv=C3=A9=20Poussineau?= To: qemu-devel@nongnu.org Date: Thu, 2 May 2013 22:08:59 +0200 Message-Id: <1367525344-7755-3-git-send-email-hpoussin@reactos.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1367525344-7755-1-git-send-email-hpoussin@reactos.org> References: <1367525344-7755-1-git-send-email-hpoussin@reactos.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:e0c:1:1599::10 Cc: =?UTF-8?q?Herv=C3=A9=20Poussineau?= , qemu-ppc@nongnu.org, =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [Qemu-devel] [PATCH 2/7] qom: handle registration of new types when initializing the first ones X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org When initializing all types in object_class_foreach, called by object_class_get_list, some new types may be registered. Those will change the type internal hashtable which is currently enumerated, and may crash QEMU. Fix it, by adding a second hash table which contains all the non-initialized types, merged to the main one before each round of initializations. Bug has been detected when registering dynamic types containing an interface. Signed-off-by: Hervé Poussineau --- qom/object.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/qom/object.c b/qom/object.c index 75e6aac..e0a24dc 100644 --- a/qom/object.c +++ b/qom/object.c @@ -65,25 +65,39 @@ struct TypeImpl static Type type_interface; +static GHashTable *type_table_to_initialize; +static GHashTable *type_table_initialized; + static GHashTable *type_table_get(void) { - static GHashTable *type_table; - - if (type_table == NULL) { - type_table = g_hash_table_new(g_str_hash, g_str_equal); + if (!type_table_initialized) { + type_table_initialized = g_hash_table_new(g_str_hash, g_str_equal); } - return type_table; + return type_table_initialized; } static void type_table_add(TypeImpl *ti) { - g_hash_table_insert(type_table_get(), (void *)ti->name, ti); + GHashTable **type_table; + if (ti->class) { + type_table = &type_table_initialized; + } else { + type_table = &type_table_to_initialize; + } + if (!*type_table) { + *type_table = g_hash_table_new(g_str_hash, g_str_equal); + } + g_hash_table_insert(*type_table, (void *)ti->name, ti); } static TypeImpl *type_table_lookup(const char *name) { - return g_hash_table_lookup(type_table_get(), name); + TypeImpl *ret = g_hash_table_lookup(type_table_get(), name); + if (!ret && type_table_to_initialize) { + ret = g_hash_table_lookup(type_table_to_initialize, name); + } + return ret; } static TypeImpl *type_register_internal(const TypeInfo *info) @@ -573,13 +587,28 @@ static void object_class_foreach_tramp(gpointer key, gpointer value, data->fn(k, data->opaque); } +static void object_class_merge(gpointer key, gpointer value, + gpointer opaque) +{ + g_hash_table_insert(type_table_get(), key, value); +} + void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), const char *implements_type, bool include_abstract, void *opaque) { OCFData data = { fn, implements_type, include_abstract, opaque }; - g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); + while (type_table_to_initialize && + g_hash_table_size(type_table_to_initialize) > 0) { + g_hash_table_foreach(type_table_to_initialize, object_class_merge, + NULL); + g_hash_table_destroy(type_table_to_initialize); + type_table_to_initialize = NULL; + + g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, + &data); + } } int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),