All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: famz@redhat.com, qemu-devel@nongnu.org,
	Luiz Capitulino <lcapitulino@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v3 04/18] qapi: Factor out JSON number formatting
Date: Tue, 03 May 2016 10:02:44 +0200	[thread overview]
Message-ID: <87bn4nbmcb.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <572364E7.3000002@redhat.com> (Eric Blake's message of "Fri, 29 Apr 2016 07:43:03 -0600")

Eric Blake <eblake@redhat.com> writes:

> On 04/29/2016 07:22 AM, Markus Armbruster wrote:
>> Eric Blake <eblake@redhat.com> writes:
>> 
>>> Pull out a new qstring_append_json_number() helper, so that all
>>> JSON output producers can use a consistent style for printing
>>> floating point without duplicating code (since we are doing more
>>> data massaging than a simple printf format can handle).
>>>
>>> Address one FIXME by adding an Error parameter and warning the
>>> caller if the requested number cannot be represented in JSON;
>>> but add another FIXME in its place because we have no way to
>>> report the problem higher up the stack.
>>>
>>> Signed-off-by: Eric Blake <eblake@redhat.com>
>>> Reviewed-by: Fam Zheng <famz@redhat.com>
>>>
>
>>>  /**
>>> + * qstring_append_json_number(): Append a JSON number to a QString.
>>> + * Set @errp if the number is not representable in JSON, but append the
>>> + * output anyway (callers can then choose to ignore the warning).
>>> + */
>>> +void qstring_append_json_number(QString *qstring, double number, Error **errp)
>>> +{
>>> +    char buffer[1024];
>>> +    int len;
>>> +
>>> +    /* JSON does not allow Inf or NaN; append it but set errp */
>>> +    if (!isfinite(number)) {
>>> +        error_setg(errp, "Non-finite number %f is not valid JSON", number);
>>> +    }
>> 
>> Separate patch, please.
>
> Okay.
>
>> 
>> "Append it but set errp" feels odd.  Normally, returning with an error
>> set means the function failed to do its job.
>
> This one's weird because by the end of the series, it will be used by
> the new JSON visitor (which wants the error message because that is not
> valid JSON, and doesn't care if the QString is slightly longer); as well
> as the existing QMP output visitor (where existing behavior ignores that
> it is not valid JSON, and we don't really have a convenient way to pass
> errors back up the stack).  Is it worth trying to plumb in better error
> reporting to the QMP output visitor, and/or add assertions that values
> are finite, and/or document that QMP has an extension beyond JSON in
> that it accepts and also might produce Inf/NaN?

QMP is what it is.  We can try to get it back closer to pure JSON, and
declaring attempts to emit non-finite numbers bugs would be a step in
that direction.

What kind of bugs would that be?

If it's a programming bug, then assert.  How would a user of the QMP
output visitor avoid this programming bug?

If it's not a programming bug, then fail with error set.  How would a
user of the QMP output visitor handle this error?

That said, I'm reluctant to mess with QMP now.  We really, really need
to control the scope of your ongoing QAPI work, or we'll never get to
Marc-André's.

Instead, I'd prefer to document what we have.  If we plan to change it,
say so, and add a suitable TODO or FIXME comment.

With things kept as they are, I can see two ways to avoid
qstring_append_json_number()'s unusual behavior:

1. Add a @must_be_finite parameter.  Set the error only when it's true,
and don't do anything else then.

2. Lift the decision whether non-finite is okay into the callers.
Instead of

    // non-finite is okay
    qstring_append_json_number(qs, num, NULL);

    // non-finite is not okay
    qstring_append_json_number(qs, num, &err);
    if (err) {
        // handle error...
        // return, goto out, or similar
    }

do

    // non-finite is okay
    qstring_append_json_number(qs, num);

    // non-finite is not okay
    if (!isfinite(num) {
        error_setg(&err, "Non-finite number %f is not valid JSON", num);
        // handle error...
        // return, goto out, or similar
    }
    qstring_append_json_number(qs, num);

>>> +
>>> +    /* FIXME: snprintf() is locale dependent; but JSON requires
>>> +     * numbers to be formatted as if in the C locale. Dependence
>>> +     * on C locale is a pervasive issue in QEMU. */
>>> +    /* FIXME: This risks printing Inf or NaN, which are not valid
>>> +     * JSON values. */
>> 
>> Your !isfinite() conditional addresses this, doesn't it?
>
> Yep. Looks like I messed up the rebase (I realized I had to re-move
> updated code, but didn't scrub the comments after the move).
>
>
>> 
>> I think this belongs into qobject-json.c, like the previous patch's
>> qstring_append_json_string().
>
> Sounds reasonable.

  reply	other threads:[~2016-05-03  8:03 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-29  4:23 [Qemu-devel] [PATCH v3 00/18] Add qapi-to-JSON and clone visitors Eric Blake
2016-04-29  4:23 ` [PATCH v3 01/18] qapi: Rename (one) qjson.h to qobject-json.h Eric Blake
2016-04-29  4:23   ` [Qemu-devel] " Eric Blake
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 02/18] qapi: Improve use of qmp/types.h Eric Blake
2016-04-29 11:46   ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 03/18] qapi: Factor out JSON string escaping Eric Blake
2016-04-29 12:09   ` Markus Armbruster
2016-04-29 17:57     ` Eric Blake
2016-05-03  7:36       ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 04/18] qapi: Factor out JSON number formatting Eric Blake
2016-04-29 13:22   ` Markus Armbruster
2016-04-29 13:43     ` Eric Blake
2016-05-03  8:02       ` Markus Armbruster [this message]
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 05/18] qapi: Use qstring_append_chr() where appropriate Eric Blake
2016-04-29 13:25   ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 06/18] qapi: Add qstring_append_format() Eric Blake
2016-04-29 13:40   ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 07/18] qapi: Add json output visitor Eric Blake
2016-05-02  9:15   ` Markus Armbruster
2016-05-02 15:11     ` Eric Blake
2016-05-03  8:22       ` Markus Armbruster
2016-05-04 15:45         ` Markus Armbruster
2016-05-06  4:16           ` Eric Blake
2016-05-06 12:31             ` Markus Armbruster
2016-05-06 14:08               ` Eric Blake
2016-05-10  4:22                 ` Eric Blake
2016-05-18 15:16     ` Eric Blake
2016-05-18 15:24       ` Eric Blake
2016-05-02 15:00   ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 08/18] qjson: Simplify by using json-output-visitor Eric Blake
2016-05-02 12:45   ` Markus Armbruster
2016-05-02 12:49     ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 09/18] Revert "qjson: Simplify by using json-output-visitor" Eric Blake
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 10/18] vmstate: Use new JSON output visitor Eric Blake
2016-05-02 13:26   ` Markus Armbruster
2016-05-02 14:23     ` Eric Blake
2016-05-03  8:30       ` Markus Armbruster
2016-05-03  9:44   ` Dr. David Alan Gilbert
2016-05-03 12:26     ` Markus Armbruster
2016-05-03 12:34       ` Eric Blake
2016-05-03 13:27         ` Dr. David Alan Gilbert
2016-05-04  8:39           ` Markus Armbruster
2016-05-04  8:54             ` Dr. David Alan Gilbert
2016-05-24  7:15               ` Paolo Bonzini
2016-05-03 13:23       ` Dr. David Alan Gilbert
2016-05-04  9:11         ` Markus Armbruster
2016-05-04  9:22           ` Dr. David Alan Gilbert
2016-05-04 11:37             ` Markus Armbruster
2016-05-04 11:56               ` Dr. David Alan Gilbert
2016-05-04 13:00                 ` Markus Armbruster
2016-05-04 13:19                   ` Dr. David Alan Gilbert
2016-05-04 14:10                     ` Markus Armbruster
2016-05-04 14:53                       ` Dr. David Alan Gilbert
2016-05-04 15:17                         ` Eric Blake
2016-05-04 15:42                         ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 11/18] qjson: Remove unused file Eric Blake
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 12/18] qapi: Add qobject_to_json_pretty_prefix() Eric Blake
2016-05-02 13:56   ` Markus Armbruster
2016-05-02 15:14     ` Eric Blake
2016-05-03  8:32       ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 13/18] qapi: Support pretty printing in JSON output visitor Eric Blake
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 14/18] qemu-img: Use new JSON output formatter Eric Blake
2016-05-02 14:04   ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 15/18] qapi: Add new clone visitor Eric Blake
2016-05-02 17:54   ` Markus Armbruster
2016-05-02 19:25     ` Eric Blake
2016-05-03 11:36       ` Markus Armbruster
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 16/18] sockets: Use new QAPI cloning Eric Blake
2016-04-29  8:30   ` Daniel P. Berrange
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 17/18] replay: " Eric Blake
2016-04-29  4:23 ` [Qemu-devel] [PATCH v3 18/18] qapi: Add parameter to visit_end_* Eric Blake
2016-05-02 18:20   ` Markus Armbruster
2016-05-02 19:31     ` Eric Blake
2016-05-03 11:53       ` Markus Armbruster
2016-05-03 12:41         ` Eric Blake
2016-05-09  8:50 ` [Qemu-devel] [PATCH v3 00/18] Add qapi-to-JSON and clone visitors Paolo Bonzini
2016-05-09  9:29   ` Paolo Bonzini
2016-05-09 14:52     ` Eric Blake

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=87bn4nbmcb.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=lcapitulino@redhat.com \
    --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.