26ba25
From 5d1c329444f36191b0e868f2a43178d289ba1f59 Mon Sep 17 00:00:00 2001
26ba25
From: Markus Armbruster <armbru@redhat.com>
26ba25
Date: Mon, 18 Jun 2018 08:43:24 +0200
26ba25
Subject: [PATCH 026/268] block-qdict: Clean up qdict_crumple() a bit
26ba25
26ba25
RH-Author: Markus Armbruster <armbru@redhat.com>
26ba25
Message-id: <20180618084330.30009-18-armbru@redhat.com>
26ba25
Patchwork-id: 80729
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 17/23] block-qdict: Clean up qdict_crumple() a bit
26ba25
Bugzilla: 1557995
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
26ba25
When you mix scalar and non-scalar keys, whether you get an "already
26ba25
set as scalar" or an "already set as dict" error depends on qdict
26ba25
iteration order.  Neither message makes much sense.  Replace by
26ba25
""Cannot mix scalar and non-scalar keys".  This is similar to the
26ba25
message we get for mixing list and non-list keys.
26ba25
26ba25
I find qdict_crumple()'s first loop hard to understand.  Rearrange it
26ba25
and add a comment.
26ba25
26ba25
Signed-off-by: Markus Armbruster <armbru@redhat.com>
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
(cherry picked from commit 3692b5d76819e573dedc9004c4b2b0e3dad83530)
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 qobject/block-qdict.c | 32 ++++++++++++++++----------------
26ba25
 1 file changed, 16 insertions(+), 16 deletions(-)
26ba25
26ba25
diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c
26ba25
index a4e1c8d..36cf58a 100644
26ba25
--- a/qobject/block-qdict.c
26ba25
+++ b/qobject/block-qdict.c
26ba25
@@ -403,7 +403,7 @@ static int qdict_is_list(QDict *maybe_list, Error **errp)
26ba25
 QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
 {
26ba25
     const QDictEntry *ent;
26ba25
-    QDict *two_level, *multi_level = NULL;
26ba25
+    QDict *two_level, *multi_level = NULL, *child_dict;
26ba25
     QObject *dst = NULL, *child;
26ba25
     size_t i;
26ba25
     char *prefix = NULL;
26ba25
@@ -422,28 +422,28 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
         }
26ba25
 
26ba25
         qdict_split_flat_key(ent->key, &prefix, &suffix);
26ba25
-
26ba25
         child = qdict_get(two_level, prefix);
26ba25
+        child_dict = qobject_to(QDict, child);
26ba25
+
26ba25
+        if (child) {
26ba25
+            /*
26ba25
+             * If @child_dict, then all previous keys with this prefix
26ba25
+             * had a suffix.  If @suffix, this one has one as well,
26ba25
+             * and we're good, else there's a clash.
26ba25
+             */
26ba25
+            if (!child_dict || !suffix) {
26ba25
+                error_setg(errp, "Cannot mix scalar and non-scalar keys");
26ba25
+                goto error;
26ba25
+            }
26ba25
+        }
26ba25
+
26ba25
         if (suffix) {
26ba25
-            QDict *child_dict = qobject_to(QDict, child);
26ba25
             if (!child_dict) {
26ba25
-                if (child) {
26ba25
-                    error_setg(errp, "Key %s prefix is already set as a scalar",
26ba25
-                               prefix);
26ba25
-                    goto error;
26ba25
-                }
26ba25
-
26ba25
                 child_dict = qdict_new();
26ba25
-                qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
26ba25
+                qdict_put(two_level, prefix, child_dict);
26ba25
             }
26ba25
-
26ba25
             qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
26ba25
         } else {
26ba25
-            if (child) {
26ba25
-                error_setg(errp, "Key %s prefix is already set as a dict",
26ba25
-                           prefix);
26ba25
-                goto error;
26ba25
-            }
26ba25
             qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
26ba25
         }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25