From patchwork Fri Apr 29 04:23:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8976981 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 791929F372 for ; Fri, 29 Apr 2016 04:26:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A9B7F20219 for ; Fri, 29 Apr 2016 04:26:19 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A6FBF201C0 for ; Fri, 29 Apr 2016 04:26:18 +0000 (UTC) Received: from localhost ([::1]:52214 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw00Q-0003Dd-0N for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 00:26:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37710) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy1-00076o-FB for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avzy0-0000ra-3J for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59608) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzxz-0000rV-TD for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:48 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 98D1862640 for ; Fri, 29 Apr 2016 04:23:47 +0000 (UTC) Received: from red.redhat.com (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3T4Ngf0016324; Fri, 29 Apr 2016 00:23:47 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 28 Apr 2016 22:23:24 -0600 Message-Id: <1461903820-3092-4-git-send-email-eblake@redhat.com> In-Reply-To: <1461903820-3092-1-git-send-email-eblake@redhat.com> References: <1461903820-3092-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 29 Apr 2016 04:23:47 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 03/18] qapi: Factor out JSON string escaping X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: famz@redhat.com, armbru@redhat.com, Luiz Capitulino Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Pull out a new qstring_append_json_string() helper, so that all JSON output producers can use the same output escaping rules. While it appears that vmstate's use of the simpler qjson.c formatter is not currently encountering any string that needs escapes to be valid JSON, it is better to be safe than sorry. Signed-off-by: Eric Blake Reviewed-by: Fam Zheng --- v3: rebase to include cleanups in master v2: no change --- include/qapi/qmp/qstring.h | 1 + qjson.c | 9 +++---- qobject/qobject-json.c | 59 ++------------------------------------------- qobject/qstring.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h index 10076b7..a254ee3 100644 --- a/include/qapi/qmp/qstring.h +++ b/include/qapi/qmp/qstring.h @@ -30,6 +30,7 @@ const char *qstring_get_str(const QString *qstring); void qstring_append_int(QString *qstring, int64_t value); void qstring_append(QString *qstring, const char *str); void qstring_append_chr(QString *qstring, int c); +void qstring_append_json_string(QString *qstring, const char *raw); QString *qobject_to_qstring(const QObject *obj); void qstring_destroy_obj(QObject *obj); diff --git a/qjson.c b/qjson.c index b65ca6e..b9a9a36 100644 --- a/qjson.c +++ b/qjson.c @@ -36,9 +36,8 @@ static void json_emit_element(QJSON *json, const char *name) } if (name) { - qstring_append(json->str, "\""); - qstring_append(json->str, name); - qstring_append(json->str, "\" : "); + qstring_append_json_string(json->str, name); + qstring_append(json->str, " : "); } } @@ -77,9 +76,7 @@ void json_prop_int(QJSON *json, const char *name, int64_t val) void json_prop_str(QJSON *json, const char *name, const char *str) { json_emit_element(json, name); - qstring_append_chr(json->str, '"'); - qstring_append(json->str, str); - qstring_append_chr(json->str, '"'); + qstring_append_json_string(json->str, str); } const char *qjson_get_str(QJSON *json) diff --git a/qobject/qobject-json.c b/qobject/qobject-json.c index 24e7d80..6fed1ee 100644 --- a/qobject/qobject-json.c +++ b/qobject/qobject-json.c @@ -16,7 +16,6 @@ #include "qapi/qmp/json-parser.h" #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/qobject-json.h" -#include "qemu/unicode.h" #include "qapi/qmp/types.h" typedef struct JSONParsingState @@ -81,7 +80,6 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent); static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) { ToJsonIterState *s = opaque; - QString *qkey; int j; if (s->count) { @@ -94,9 +92,7 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) qstring_append(s->str, " "); } - qkey = qstring_from_str(key); - to_json(QOBJECT(qkey), s->str, s->pretty, s->indent); - QDECREF(qkey); + qstring_append_json_string(s->str, key); qstring_append(s->str, ": "); to_json(obj, s->str, s->pretty, s->indent); @@ -138,58 +134,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) } case QTYPE_QSTRING: { QString *val = qobject_to_qstring(obj); - const char *ptr; - int cp; - char buf[16]; - char *end; - - ptr = qstring_get_str(val); - qstring_append(str, "\""); - - for (; *ptr; ptr = end) { - cp = mod_utf8_codepoint(ptr, 6, &end); - switch (cp) { - case '\"': - qstring_append(str, "\\\""); - break; - case '\\': - qstring_append(str, "\\\\"); - break; - case '\b': - qstring_append(str, "\\b"); - break; - case '\f': - qstring_append(str, "\\f"); - break; - case '\n': - qstring_append(str, "\\n"); - break; - case '\r': - qstring_append(str, "\\r"); - break; - case '\t': - qstring_append(str, "\\t"); - break; - default: - if (cp < 0) { - cp = 0xFFFD; /* replacement character */ - } - if (cp > 0xFFFF) { - /* beyond BMP; need a surrogate pair */ - snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", - 0xD800 + ((cp - 0x10000) >> 10), - 0xDC00 + ((cp - 0x10000) & 0x3FF)); - } else if (cp < 0x20 || cp >= 0x7F) { - snprintf(buf, sizeof(buf), "\\u%04X", cp); - } else { - buf[0] = cp; - buf[1] = 0; - } - qstring_append(str, buf); - } - }; - - qstring_append(str, "\""); + qstring_append_json_string(str, qstring_get_str(val)); break; } case QTYPE_QDICT: { diff --git a/qobject/qstring.c b/qobject/qstring.c index 5da7b5f..9f0df5b 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -14,6 +14,7 @@ #include "qapi/qmp/qobject.h" #include "qapi/qmp/qstring.h" #include "qemu-common.h" +#include "qemu/unicode.h" /** * qstring_new(): Create a new empty QString @@ -107,6 +108,65 @@ void qstring_append_chr(QString *qstring, int c) } /** + * qstring_append_json_string(): Append a raw string to a QString, while + * adding outer "" and JSON escape sequences. + */ +void qstring_append_json_string(QString *qstring, const char *raw) +{ + const char *ptr = raw; + int cp; + char buf[16]; + char *end; + + qstring_append(qstring, "\""); + + for (; *ptr; ptr = end) { + cp = mod_utf8_codepoint(ptr, 6, &end); + switch (cp) { + case '\"': + qstring_append(qstring, "\\\""); + break; + case '\\': + qstring_append(qstring, "\\\\"); + break; + case '\b': + qstring_append(qstring, "\\b"); + break; + case '\f': + qstring_append(qstring, "\\f"); + break; + case '\n': + qstring_append(qstring, "\\n"); + break; + case '\r': + qstring_append(qstring, "\\r"); + break; + case '\t': + qstring_append(qstring, "\\t"); + break; + default: + if (cp < 0) { + cp = 0xFFFD; /* replacement character */ + } + if (cp > 0xFFFF) { + /* beyond BMP; need a surrogate pair */ + snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", + 0xD800 + ((cp - 0x10000) >> 10), + 0xDC00 + ((cp - 0x10000) & 0x3FF)); + } else if (cp < 0x20 || cp >= 0x7F) { + snprintf(buf, sizeof(buf), "\\u%04X", cp); + } else { + buf[0] = cp; + buf[1] = 0; + } + qstring_append(qstring, buf); + } + }; + + qstring_append(qstring, "\""); +} + +/** * qobject_to_qstring(): Convert a QObject to a QString */ QString *qobject_to_qstring(const QObject *obj)