From nobody Thu Apr 17 09:23:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org> Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1529073405776979.650437526299; Fri, 15 Jun 2018 07:36:45 -0700 (PDT) Received: from localhost ([::1]:47213 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from <qemu-devel-bounces+importer=patchew.org@nongnu.org>) id 1fTpqG-0002vj-Vk for importer@patchew.org; Fri, 15 Jun 2018 10:36:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59285) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from <kwolf@redhat.com>) id 1fTpbd-0006Cd-GU for qemu-devel@nongnu.org; Fri, 15 Jun 2018 10:21:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from <kwolf@redhat.com>) id 1fTpbb-0003n2-B1 for qemu-devel@nongnu.org; Fri, 15 Jun 2018 10:21:37 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:50226 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from <kwolf@redhat.com>) id 1fTpbW-0003hQ-GG; Fri, 15 Jun 2018 10:21:30 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2B64081A3245; Fri, 15 Jun 2018 14:21:30 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-70.ams2.redhat.com [10.36.117.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B6DA6352E; Fri, 15 Jun 2018 14:21:29 +0000 (UTC) From: Kevin Wolf <kwolf@redhat.com> To: qemu-block@nongnu.org Date: Fri, 15 Jun 2018 16:21:02 +0200 Message-Id: <20180615142108.27814-21-kwolf@redhat.com> In-Reply-To: <20180615142108.27814-1-kwolf@redhat.com> References: <20180615142108.27814-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 15 Jun 2018 14:21:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 15 Jun 2018 14:21:30 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 20/26] block: Fix -blockdev / blockdev-add for empty objects and arrays X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: <qemu-devel.nongnu.org> List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>, <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe> List-Archive: <http://lists.nongnu.org/archive/html/qemu-devel/> List-Post: <mailto:qemu-devel@nongnu.org> List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help> List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>, <mailto:qemu-devel-request@nongnu.org?subject=subscribe> Cc: kwolf@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" <qemu-devel-bounces+importer=patchew.org@nongnu.org> X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Markus Armbruster <armbru@redhat.com> -blockdev and blockdev-add silently ignore empty objects and arrays in their argument. That's because qmp_blockdev_add() converts the argument to a flat QDict, and qdict_flatten() eats empty QDict and QList members. For instance, we ignore an empty BlockdevOptions member @cache. No real harm, as absent means the same as empty there. Thus, the flaw puts an artificial restriction on the QAPI schema: we can't have potentially empty objects and arrays within BlockdevOptions, except when they're optional and "empty" has the same meaning as "absent". Our QAPI schema satisfies this restriction (I checked), but it's a trap for the unwary, and a temptation to employ awkward workarounds for the wary. Let's get rid of it. Change qdict_flatten() and qdict_crumple() to treat empty dictionaries and lists exactly like scalars. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com> --- qobject/block-qdict.c | 54 +++++++++++++++++++++++++++++--------------= ---- tests/check-block-qdict.c | 38 ++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c index e51a3d2c0f..df833083a7 100644 --- a/qobject/block-qdict.c +++ b/qobject/block-qdict.c @@ -56,6 +56,8 @@ static void qdict_flatten_qlist(QList *qlist, QDict *targ= et, const char *prefix) { QObject *value; const QListEntry *entry; + QDict *dict_val; + QList *list_val; char *new_key; int i; =20 @@ -69,16 +71,18 @@ static void qdict_flatten_qlist(QList *qlist, QDict *ta= rget, const char *prefix) =20 for (i =3D 0; entry; entry =3D qlist_next(entry), i++) { value =3D qlist_entry_obj(entry); + dict_val =3D qobject_to(QDict, value); + list_val =3D qobject_to(QList, value); new_key =3D g_strdup_printf("%s.%i", prefix, i); =20 /* * Flatten non-empty QDict and QList recursively into @target, * copy other objects to @target */ - if (qobject_type(value) =3D=3D QTYPE_QDICT) { - qdict_flatten_qdict(qobject_to(QDict, value), target, new_key); - } else if (qobject_type(value) =3D=3D QTYPE_QLIST) { - qdict_flatten_qlist(qobject_to(QList, value), target, new_key); + if (dict_val && qdict_size(dict_val)) { + qdict_flatten_qdict(dict_val, target, new_key); + } else if (list_val && !qlist_empty(list_val)) { + qdict_flatten_qlist(list_val, target, new_key); } else { qdict_put_obj(target, new_key, qobject_ref(value)); } @@ -91,6 +95,8 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *targ= et, const char *prefix) { QObject *value; const QDictEntry *entry, *next; + QDict *dict_val; + QList *list_val; char *new_key; =20 entry =3D qdict_first(qdict); @@ -98,6 +104,8 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *tar= get, const char *prefix) while (entry !=3D NULL) { next =3D qdict_next(qdict, entry); value =3D qdict_entry_value(entry); + dict_val =3D qobject_to(QDict, value); + list_val =3D qobject_to(QList, value); new_key =3D NULL; =20 if (prefix) { @@ -108,12 +116,12 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *= target, const char *prefix) * Flatten non-empty QDict and QList recursively into @target, * copy other objects to @target */ - if (qobject_type(value) =3D=3D QTYPE_QDICT) { - qdict_flatten_qdict(qobject_to(QDict, value), target, + if (dict_val && qdict_size(dict_val)) { + qdict_flatten_qdict(dict_val, target, new_key ? new_key : entry->key); qdict_del(qdict, entry->key); - } else if (qobject_type(value) =3D=3D QTYPE_QLIST) { - qdict_flatten_qlist(qobject_to(QList, value), target, + } else if (list_val && !qlist_empty(list_val)) { + qdict_flatten_qlist(list_val, target, new_key ? new_key : entry->key); qdict_del(qdict, entry->key); } else if (target !=3D qdict) { @@ -127,10 +135,11 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *= target, const char *prefix) } =20 /** - * qdict_flatten(): For each nested QDict with key x, all fields with key y - * are moved to this QDict and their key is renamed to "x.y". For each nes= ted - * QList with key x, the field at index y is moved to this QDict with the = key - * "x.y" (i.e., the reverse of what qdict_array_split() does). + * qdict_flatten(): For each nested non-empty QDict with key x, all + * fields with key y are moved to this QDict and their key is renamed + * to "x.y". For each nested non-empty QList with key x, the field at + * index y is moved to this QDict with the key "x.y" (i.e., the + * reverse of what qdict_array_split() does). * This operation is applied recursively for nested QDicts and QLists. */ void qdict_flatten(QDict *qdict) @@ -361,8 +370,8 @@ static int qdict_is_list(QDict *maybe_list, Error **err= p) * @src: the original flat dictionary (only scalar values) to crumple * * Takes a flat dictionary whose keys use '.' separator to indicate - * nesting, and values are scalars, and crumples it into a nested - * structure. + * nesting, and values are scalars, empty dictionaries or empty lists, + * and crumples it into a nested structure. * * To include a literal '.' in a key name, it must be escaped as '..' * @@ -399,6 +408,8 @@ QObject *qdict_crumple(const QDict *src, Error **errp) { const QDictEntry *ent; QDict *two_level, *multi_level =3D NULL, *child_dict; + QDict *dict_val; + QList *list_val; QObject *dst =3D NULL, *child; size_t i; char *prefix =3D NULL; @@ -409,10 +420,11 @@ QObject *qdict_crumple(const QDict *src, Error **errp) =20 /* Step 1: split our totally flat dict into a two level dict */ for (ent =3D qdict_first(src); ent !=3D NULL; ent =3D qdict_next(src, = ent)) { - if (qobject_type(ent->value) =3D=3D QTYPE_QDICT || - qobject_type(ent->value) =3D=3D QTYPE_QLIST) { - error_setg(errp, "Value %s is not a scalar", - ent->key); + dict_val =3D qobject_to(QDict, ent->value); + list_val =3D qobject_to(QList, ent->value); + if ((dict_val && qdict_size(dict_val)) + || (list_val && !qlist_empty(list_val))) { + error_setg(errp, "Value %s is not flat", ent->key); goto error; } =20 @@ -451,9 +463,9 @@ QObject *qdict_crumple(const QDict *src, Error **errp) multi_level =3D qdict_new(); for (ent =3D qdict_first(two_level); ent !=3D NULL; ent =3D qdict_next(two_level, ent)) { - QDict *dict =3D qobject_to(QDict, ent->value); - if (dict) { - child =3D qdict_crumple(dict, errp); + dict_val =3D qobject_to(QDict, ent->value); + if (dict_val && qdict_size(dict_val)) { + child =3D qdict_crumple(dict_val, errp); if (!child) { goto error; } diff --git a/tests/check-block-qdict.c b/tests/check-block-qdict.c index 2da16f01a6..1d20fccbd4 100644 --- a/tests/check-block-qdict.c +++ b/tests/check-block-qdict.c @@ -79,10 +79,10 @@ static void qdict_flatten_test(void) * "e.1.2.b": 1, * "f.c": 2, * "f.d": 3, - * "g": 4 + * "g": 4, + * "y.0": {}, + * "z.a": [] * } - * - * Note that "y" and "z" get eaten. */ =20 qdict_put_int(e_1_2, "a", 0); @@ -117,8 +117,10 @@ static void qdict_flatten_test(void) g_assert(qdict_get_int(root, "f.c") =3D=3D 2); g_assert(qdict_get_int(root, "f.d") =3D=3D 3); g_assert(qdict_get_int(root, "g") =3D=3D 4); + g_assert(!qdict_size(qdict_get_qdict(root, "y.0"))); + g_assert(qlist_empty(qdict_get_qlist(root, "z.a"))); =20 - g_assert(qdict_size(root) =3D=3D 8); + g_assert(qdict_size(root) =3D=3D 10); =20 qobject_unref(root); } @@ -387,7 +389,8 @@ static void qdict_join_test(void) static void qdict_crumple_test_recursive(void) { QDict *src, *dst, *rule, *vnc, *acl, *listen; - QList *rules; + QDict *empty, *empty_dict, *empty_list_0; + QList *rules, *empty_list, *empty_dict_a; =20 src =3D qdict_new(); qdict_put_str(src, "vnc.listen.addr", "127.0.0.1"); @@ -399,10 +402,12 @@ static void qdict_crumple_test_recursive(void) qdict_put_str(src, "vnc.acl.default", "deny"); qdict_put_str(src, "vnc.acl..name", "acl0"); qdict_put_str(src, "vnc.acl.rule..name", "acl0"); + qdict_put(src, "empty.dict.a", qlist_new()); + qdict_put(src, "empty.list.0", qdict_new()); =20 dst =3D qobject_to(QDict, qdict_crumple(src, &error_abort)); g_assert(dst); - g_assert_cmpint(qdict_size(dst), =3D=3D, 1); + g_assert_cmpint(qdict_size(dst), =3D=3D, 2); =20 vnc =3D qdict_get_qdict(dst, "vnc"); g_assert(vnc); @@ -440,6 +445,21 @@ static void qdict_crumple_test_recursive(void) g_assert_cmpstr("acl0", =3D=3D, qdict_get_str(vnc, "acl.name")); g_assert_cmpstr("acl0", =3D=3D, qdict_get_str(acl, "rule.name")); =20 + empty =3D qdict_get_qdict(dst, "empty"); + g_assert(empty); + g_assert_cmpint(qdict_size(empty), =3D=3D, 2); + empty_dict =3D qdict_get_qdict(empty, "dict"); + g_assert(empty_dict); + g_assert_cmpint(qdict_size(empty_dict), =3D=3D, 1); + empty_dict_a =3D qdict_get_qlist(empty_dict, "a"); + g_assert(empty_dict_a && qlist_empty(empty_dict_a)); + empty_list =3D qdict_get_qlist(empty, "list"); + g_assert(empty_list); + g_assert_cmpint(qlist_size(empty_list), =3D=3D, 1); + empty_list_0 =3D qobject_to(QDict, qlist_pop(empty_list)); + g_assert(empty_list_0); + g_assert_cmpint(qdict_size(empty_list_0), =3D=3D, 0); + qobject_unref(src); qobject_unref(dst); } @@ -587,7 +607,7 @@ static void qdict_rename_keys_test(void) =20 static void qdict_crumple_test_bad_inputs(void) { - QDict *src; + QDict *src, *nested; Error *error =3D NULL; =20 src =3D qdict_new(); @@ -614,7 +634,9 @@ static void qdict_crumple_test_bad_inputs(void) =20 src =3D qdict_new(); /* The input should be flat, ie no dicts or lists */ - qdict_put(src, "rule.a", qdict_new()); + nested =3D qdict_new(); + qdict_put(nested, "x", qdict_new()); + qdict_put(src, "rule.a", nested); qdict_put_str(src, "rule.b", "allow"); =20 g_assert(qdict_crumple(src, &error) =3D=3D NULL); --=20 2.13.6