qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] json: escape u0000 .. u001F when outputting json
@ 2010-01-25 15:00 Anthony Liguori
  2010-01-25 15:48 ` [Qemu-devel] " Markus Armbruster
  0 siblings, 1 reply; 2+ messages in thread
From: Anthony Liguori @ 2010-01-25 15:00 UTC (permalink / raw)
  To: qemu-devel; +Cc: Anthony Liguori, Markus Armbruster

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>
---
 qjson.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/qjson.c b/qjson.c
index 60c904d..9ad8a91 100644
--- a/qjson.c
+++ b/qjson.c
@@ -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;
                 }
                 }
-- 
1.6.5.2

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [Qemu-devel] Re: [PATCH] json: escape u0000 .. u001F when outputting json
  2010-01-25 15:00 [Qemu-devel] [PATCH] json: escape u0000 .. u001F when outputting json Anthony Liguori
@ 2010-01-25 15:48 ` Markus Armbruster
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Armbruster @ 2010-01-25 15:48 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: qemu-devel

Anthony Liguori <aliguori@us.ibm.com> writes:

> 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.

Looks good.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-01-25 15:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-25 15:00 [Qemu-devel] [PATCH] json: escape u0000 .. u001F when outputting json Anthony Liguori
2010-01-25 15:48 ` [Qemu-devel] " Markus Armbruster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).