[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 16/28] qapi: Factor out JSON string escaping
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v4 16/28] qapi: Factor out JSON string escaping |
Date: |
Wed, 18 May 2016 22:41:02 -0600 |
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,
and this substitution is not adding any potential asserts
during migration.
Adding a return value will let callers that care diagnose the
situation where an attempt was made to output invalid JSON (we
use substitute characters, and in fact guarantee that our
output is ASCII via escapes even if the input was UTF-8). None
of the current callers care, but a future patch wants to make
it possible to turn this situation into an error.
Signed-off-by: Eric Blake <address@hidden>
---
v4: keep helper in qobject-json.[ch], tweak variable name and
for loop iterator, add return value, drop R-b
v3: rebase to include cleanups in master
v2: no change
---
include/qapi/qmp/qobject-json.h | 2 +
migration/qjson.c | 10 ++--
qobject/qobject-json.c | 125 ++++++++++++++++++++++------------------
3 files changed, 75 insertions(+), 62 deletions(-)
diff --git a/include/qapi/qmp/qobject-json.h b/include/qapi/qmp/qobject-json.h
index 02b1f2c..aa8ddd7 100644
--- a/include/qapi/qmp/qobject-json.h
+++ b/include/qapi/qmp/qobject-json.h
@@ -24,4 +24,6 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap)
GCC_FMT_ATTR(1, 0);
QString *qobject_to_json(const QObject *obj);
QString *qobject_to_json_pretty(const QObject *obj);
+int qstring_append_json_string(QString *qstring, const char *str);
+
#endif /* QJSON_H */
diff --git a/migration/qjson.c b/migration/qjson.c
index 5cae55a..b053eff 100644
--- a/migration/qjson.c
+++ b/migration/qjson.c
@@ -26,6 +26,7 @@
#include "qemu/osdep.h"
#include "qapi/qmp/qstring.h"
#include "migration/qjson.h"
+#include "qapi/qmp/qobject-json.h"
struct QJSON {
QString *str;
@@ -42,9 +43,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, " : ");
}
}
@@ -83,9 +83,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..bc3634f 100644
--- a/qobject/qobject-json.c
+++ b/qobject/qobject-json.c
@@ -81,7 +81,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 +93,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 +135,9 @@ 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, "\"");
+ /* FIXME: no way inform user if we modified the string to
+ * avoid encoding errors */
+ qstring_append_json_string(str, qstring_get_str(val));
break;
}
case QTYPE_QDICT: {
@@ -290,3 +238,68 @@ QString *qobject_to_json_pretty(const QObject *obj)
return str;
}
+
+/**
+ * Append a JSON string to @qstring that encodes the C string @str.
+ *
+ * The JSON string is enclosed in double quotes and has funny
+ * characters escaped. Returns -1 if encoding errors were replaced,
+ * or 0 if the input was already valid UTF-8.
+ */
+int qstring_append_json_string(QString *qstring, const char *str)
+{
+ const char *ptr;
+ int cp;
+ char buf[16];
+ char *end;
+ int result = 0;
+
+ qstring_append(qstring, "\"");
+
+ for (ptr = str; *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 */
+ result = -1;
+ }
+ 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, "\"");
+ return result;
+}
--
2.5.5
- [Qemu-devel] [PATCH v4 06/28] opts-visitor: Favor new visit_free() function, (continued)
- [Qemu-devel] [PATCH v4 06/28] opts-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 08/28] qmp-input-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 07/28] string-input-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 01/28] qapi: Rename (one) qjson.h to qobject-json.h, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 09/28] string-output-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 04/28] qapi: Add parameter to visit_end_*, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 15/28] replay: Use new QAPI cloning, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 11/28] tests: Factor out common code in qapi output tests, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 10/28] qmp-output-visitor: Favor new visit_free() function, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 14/28] sockets: Use new QAPI cloning, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 16/28] qapi: Factor out JSON string escaping,
Eric Blake <=
- [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 23/28] tests: Test qobject_to_json() pretty formatting, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 13/28] qapi: Add new clone visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 18/28] qapi: Add qstring_append_printf(), Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 25/28] qapi: Support pretty printing in JSON output visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 26/28] qobject: Implement qobject_to_json() atop JSON visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 27/28] qapi: Add 'any' support to JSON output, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 20/28] qstring: Add qstring_consume_str(), Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 24/28] qapi: Add JSON output visitor, Eric Blake, 2016/05/19
- [Qemu-devel] [PATCH v4 22/28] qobject: Consolidate qobject_to_json() calls, Eric Blake, 2016/05/19