From patchwork Mon Jan 25 15:00:23 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 43637 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9A2D7B7C48 for ; Tue, 26 Jan 2010 02:08:00 +1100 (EST) Received: from localhost ([127.0.0.1]:50235 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NZQW9-0007jF-E6 for incoming@patchwork.ozlabs.org; Mon, 25 Jan 2010 10:06:17 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NZQQy-0004KH-8T for qemu-devel@nongnu.org; Mon, 25 Jan 2010 10:00:56 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NZQQt-0004Fq-Fu for qemu-devel@nongnu.org; Mon, 25 Jan 2010 10:00:55 -0500 Received: from [199.232.76.173] (port=46608 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NZQQt-0004FV-3g for qemu-devel@nongnu.org; Mon, 25 Jan 2010 10:00:51 -0500 Received: from e35.co.us.ibm.com ([32.97.110.153]:46758) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NZQQs-0007rS-Jo for qemu-devel@nongnu.org; Mon, 25 Jan 2010 10:00:50 -0500 Received: from d03relay04.boulder.ibm.com (d03relay04.boulder.ibm.com [9.17.195.106]) by e35.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id o0PEkoIV006406 for ; Mon, 25 Jan 2010 07:46:50 -0700 Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o0PF0W4i042840 for ; Mon, 25 Jan 2010 08:00:34 -0700 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o0PF0R7S027574 for ; Mon, 25 Jan 2010 08:00:27 -0700 Received: from localhost.localdomain (sig-9-65-247-59.mts.ibm.com [9.65.247.59]) by d03av01.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o0PF0PMf027347; Mon, 25 Jan 2010 08:00:26 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 25 Jan 2010 09:00:23 -0600 Message-Id: <1264431623-6607-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.5.2 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Markus Armbruster Subject: [Qemu-devel] [PATCH] json: escape u0000 .. u001F when outputting json X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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; } }