From patchwork Tue Jan 19 16:10:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 570030 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3F2D114030F for ; Wed, 20 Jan 2016 03:13:49 +1100 (AEDT) Received: from localhost ([::1]:37780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYuh-0001F1-2J for incoming@patchwork.ozlabs.org; Tue, 19 Jan 2016 11:13:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42258) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYrv-0004T9-Mf for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLYrp-0001y1-J1 for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43771) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYrp-0001xQ-Dw for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:49 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id BAA4F7AEBA for ; Tue, 19 Jan 2016 16:10:48 +0000 (UTC) Received: from red.redhat.com (ovpn-113-211.phx2.redhat.com [10.3.113.211]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0JGAlYF008625; Tue, 19 Jan 2016 11:10:48 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 19 Jan 2016 09:10:09 -0700 Message-Id: <1453219845-30939-2-git-send-email-eblake@redhat.com> In-Reply-To: <1453219845-30939-1-git-send-email-eblake@redhat.com> References: <1453219845-30939-1-git-send-email-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: marcandre.lureau@redhat.com, armbru@redhat.com, Luiz Capitulino Subject: [Qemu-devel] [PATCH v9 01/37] qobject: Document more shortcomings in our number handling 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 We've already documented that our JSON parsing is locale dependent; but we should also document that our JSON output has the same problem. Additionally, JSON requires finite values (you have to upgrade to JSON5 to get support for Inf or NaN), and our output risks truncating floating point numbers to the point of losing significant precision. Sadly, this series is not going to be the one that addresses these problems. Fix some trailing whitespace I noticed in the vicinity. Signed-off-by: Eric Blake Reviewed-by: Marc-André Lureau --- v9: no change v8: no change v7: new patch --- qobject/json-parser.c | 4 +++- qobject/qjson.c | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/qobject/json-parser.c b/qobject/json-parser.c index 3c5d35d..6ab98a7 100644 --- a/qobject/json-parser.c +++ b/qobject/json-parser.c @@ -1,5 +1,5 @@ /* - * JSON Parser + * JSON Parser * * Copyright IBM, Corp. 2009 * @@ -519,6 +519,8 @@ static QObject *parse_literal(JSONParserContext *ctxt) } case JSON_FLOAT: /* FIXME dependent on locale */ + /* FIXME our lexer matches RFC7159 in forbidding Inf or NaN, + * but those might be useful extensions beyond JSON */ return QOBJECT(qfloat_from_double(strtod(token->str, NULL))); default: abort(); diff --git a/qobject/qjson.c b/qobject/qjson.c index a3e6a7c..41d9d65 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -237,6 +237,12 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) char buffer[1024]; int len; + /* FIXME: snprintf() is locale dependent; but JSON requires + * numbers to be formatted as if in the C locale. */ + /* FIXME: This risks printing Inf or NaN, which are not valid + * JSON values. */ + /* FIXME: the default precision of %f may be insufficient to + * tell this number apart from others. */ len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val)); while (len > 0 && buffer[len - 1] == '0') { len--; @@ -247,7 +253,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } else { buffer[len] = 0; } - + qstring_append(str, buffer); break; }