diff options
author | Anthony Liguori | 2010-01-25 15:56:53 +0100 |
---|---|---|
committer | Anthony Liguori | 2010-01-26 21:54:59 +0100 |
commit | ff06ea219763559bec2aab26dde1cec8608405e9 (patch) | |
tree | 47721d5fd7ddf3a4018eac3f495f2cef93400379 /qjson.c | |
parent | Sparc64: fix initrd (diff) | |
download | qemu-ff06ea219763559bec2aab26dde1cec8608405e9.tar.gz qemu-ff06ea219763559bec2aab26dde1cec8608405e9.tar.xz qemu-ff06ea219763559bec2aab26dde1cec8608405e9.zip |
json: escape u0000 .. u001F when outputting json
Markus Armbruster pointed out:
JSON requires control characters in strings to be escaped. RFC 4627
section 2.5:
A string begins and ends with quotation marks. All Unicode
characters may be placed within the quotation marks except for the
characters that must be escaped: quotation mark, reverse solidus, and
the control characters (U+0000 through U+001F).
We've been quoting the special escape sequences that JSON defines but we
haven't been encoding the full control character range. This patch fixes that.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'qjson.c')
-rw-r--r-- | qjson.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -163,8 +163,14 @@ static void to_json(const QObject *obj, QString *str) qstring_append(str, "\\t"); break; default: { - char buf[2] = { ptr[0], 0 }; - qstring_append(str, buf); + if (ptr[0] <= 0x1F) { + char escape[7]; + snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]); + qstring_append(str, escape); + } else { + char buf[2] = { ptr[0], 0 }; + qstring_append(str, buf); + } break; } } |