All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, pkrempa@redhat.com,
	eblake@redhat.com, mdroth@linux.vnet.ibm.com
Subject: Re: [Qemu-devel] [PATCH v2 24/24] keyval: Support lists
Date: Tue, 28 Feb 2017 23:02:41 +0100	[thread overview]
Message-ID: <20170228220241.GD4090@noname.redhat.com> (raw)
In-Reply-To: <1488317230-26248-25-git-send-email-armbru@redhat.com>

Am 28.02.2017 um 22:27 hat Markus Armbruster geschrieben:
> Additionally permit non-negative integers as key components.  A
> dictionary's keys must either be all integers or none.  If all keys
> are integers, convert the dictionary to a list.  The set of keys must
> be [0,N].
> 
> Examples:
> 
> * list.1=goner,list.0=null,list.1=eins,list.2=zwei
>   is equivalent to JSON [ "null", "eins", "zwei" ]
> 
> * a.b.c=1,a.b.0=2
>   is inconsistent: a.b.c clashes with a.b.0
> 
> * list.0=null,list.2=eins,list.2=zwei
>   has a hole: list.1 is missing
> 
> Similar design flaw as for objects: there is no way to denote an empty
> list.  While interpreting "key absent" as empty list seems natural
> (removing a list member from the input string works when there are
> multiple ones, so why not when there's just one), it doesn't work:
> "key absent" already means "optional list absent", which isn't the
> same as "empty list present".
> 
> Update the keyval object visitor to use this a.0 syntax in error
> messages rather than the usual a[0].
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

> +/*
> + * Listify @cur recursively.
> + * Replace QDicts whose keys are all valid list indexes by QLists.
> + * @key_of_cur is the list of key fragments leading up to @cur.
> + * On success, return either @cur or its replacement.
> + * On failure, store an error through @errp and return NULL.
> + */
> +static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)
> +{
> +    GSList key_node;
> +    bool has_index, has_member;
> +    const QDictEntry *ent;
> +    QDict *qdict;
> +    QObject *val;
> +    char *key;
> +    size_t nelt;
> +    QObject **elt;
> +    int index, max_index, i;
> +    QList *list;
> +
> +    key_node.next = key_of_cur;
> +
> +    /*
> +     * Recursively listify @cur's members, and figure out whether @cur
> +     * itself is to be listified.
> +     */
> +    has_index = false;
> +    has_member = false;
> +    for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
> +        if (key_to_index(ent->key, NULL) >= 0) {
> +            has_index = true;
> +        } else {
> +            has_member = true;
> +        }
> +
> +        qdict = qobject_to_qdict(ent->value);
> +        if (!qdict) {
> +            continue;
> +        }
> +
> +        key_node.data = ent->key;
> +        val = keyval_listify(qdict, &key_node, errp);
> +        if (!val) {
> +            return NULL;
> +        }
> +        if (val != ent->value) {
> +            qdict_put_obj(cur, ent->key, val);
> +        }
> +    }
> +
> +    if (has_index && has_member) {
> +        key = reassemble_key(key_of_cur);
> +        error_setg(errp, "Parameters '%s*' used inconsistently", key);
> +        g_free(key);
> +        return NULL;
> +    }
> +    if (!has_index) {
> +        return QOBJECT(cur);
> +    }
> +
> +    /* Copy @cur's values to @elt[] */
> +    nelt = qdict_size(cur) + 1; /* one extra, for use as sentinel */
> +    elt = g_new0(QObject *, nelt);
> +    max_index = -1;
> +    for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
> +        index = key_to_index(ent->key, NULL);
> +        assert(index >= 0);
> +        if (index > max_index) {
> +            max_index = index;
> +        }
> +        /*
> +         * We iterate @nelt times.  If we get one exceeding @nelt
> +         * here, we will put less than @nelt values into @elt[],
> +         * triggering the error in the next loop.
> +         */
> +        if ((size_t)index >= nelt) {

I think this needs to be index >= nelt - 1 now...

> +            continue;
> +        }
> +        /* Even though dict keys are distinct, indexes need not be */
> +        elt[index] = ent->value;
> +    }
> +
> +    /*
> +     * Make a list from @elt[], reporting any missing elements.
> +     * If we dropped an index >= nelt in the previous loop, this loop
> +     * will run into the sentinel and report index @nelt missing.
> +     */
> +    list = qlist_new();
> +    assert(!elt[nelt-1]);       /* need the sentinel to be null */

...or this assertion can fail.

> +    for (i = 0; i < MIN(nelt, max_index + 1); i++) {
> +        if (!elt[i]) {
> +            key = reassemble_key(key_of_cur);
> +            error_setg(errp, "Parameter '%s%d' missing", key, i);
> +            g_free(key);
> +            g_free(elt);
> +            QDECREF(list);
> +            return NULL;
> +        }
> +        qobject_incref(elt[i]);
> +        qlist_append_obj(list, elt[i]);
> +    }
> +
> +    g_free(elt);
> +    return QOBJECT(list);
> +}

Kevin

  reply	other threads:[~2017-02-28 22:02 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-28 21:26 [Qemu-devel] [PATCH v2 00/24] block: Command line option -blockdev Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 01/24] test-qemu-opts: Cover qemu_opts_parse() of "no" Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 02/24] tests: Fix gcov-files-test-qemu-opts-y, gcov-files-test-logging-y Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 03/24] keyval: New keyval_parse() Markus Armbruster
2017-02-28 21:48   ` Eric Blake
2017-02-28 22:01     ` Markus Armbruster
2017-03-05 10:07       ` Markus Armbruster
2017-02-28 22:03   ` Kevin Wolf
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 04/24] qapi: qobject input visitor variant for use with keyval_parse() Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 05/24] test-keyval: Cover use with qobject input visitor Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 06/24] qapi: Factor out common part of qobject input visitor creation Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 07/24] qapi: Factor out common qobject_input_get_keyval() Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 08/24] qobject: Propagate parse errors through qobject_from_jsonv() Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 09/24] libqtest: Fix qmp() & friends to abort on JSON parse errors Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 10/24] qjson: Abort earlier on qobject_from_jsonf() misuse Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 11/24] test-qobject-input-visitor: Abort earlier on bad test input Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 12/24] qobject: Propagate parse errors through qobject_from_json() Markus Armbruster
2017-02-28 21:26 ` [Qemu-devel] [PATCH v2 13/24] block: More detailed syntax error reporting for JSON filenames Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 14/24] check-qjson: Test errors from qobject_from_json() Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 15/24] test-visitor-serialization: Pass &error_abort to qobject_from_json() Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 16/24] monitor: Assert qmp_schema_json[] is sane Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 17/24] test-qapi-util: New, covering qapi/qapi-util.c Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 18/24] qapi: New parse_qapi_name() Markus Armbruster
2017-02-28 22:04   ` Kevin Wolf
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 19/24] keyval: Restrict key components to valid QAPI names Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 20/24] qapi: New qobject_input_visitor_new_str() for convenience Markus Armbruster
2017-02-28 22:05   ` Kevin Wolf
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 21/24] block: Initial implementation of -blockdev Markus Armbruster
2017-02-28 22:05   ` Kevin Wolf
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 22/24] qapi: Improve how keyval input visitor reports unexpected dicts Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 23/24] docs/qapi-code-gen.txt: Clarify naming rules Markus Armbruster
2017-02-28 21:27 ` [Qemu-devel] [PATCH v2 24/24] keyval: Support lists Markus Armbruster
2017-02-28 22:02   ` Kevin Wolf [this message]
2017-02-28 22:11     ` Markus Armbruster
2017-02-28 22:17       ` Kevin Wolf
2017-02-28 22:56   ` Eric Blake
2017-03-01  5:47     ` Markus Armbruster
2017-02-28 21:55 ` [Qemu-devel] [PATCH v2 00/24] block: Command line option -blockdev Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170228220241.GD4090@noname.redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.