From patchwork Sat Dec 16 21:23:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 179926 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp429415dyi; Sat, 16 Dec 2023 13:24:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEeaiy2UKhRHs+xJKAk18BHzYfkIRCRatmsVsmxTGvzEXLz5+Ru9EE4PdN6qefeAKw9+snO X-Received: by 2002:a05:622a:190a:b0:425:99b5:6117 with SMTP id w10-20020a05622a190a00b0042599b56117mr21618831qtc.133.1702761853367; Sat, 16 Dec 2023 13:24:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702761853; cv=pass; d=google.com; s=arc-20160816; b=smghZSxQ221PmwkerQWxnPqcfYKJMv765kw1z5gVx6nwn9c0qVKyHchhbG55V6k5fH cn2iGBX0GmOHyuwUd63O8AUVx7mrkanIhUgx1qsR4r13YJjStaUzfh/RkMJiGgkPUkXD jg1zSFl9nRGhX3DHI71q42W9oKPksl3tSqZ9SvT7i2zF74UQ/5WFRndO+elTnpWrnyPt 2r+bBaur2VZRKj++9xLTms3AfdApCqeBIwz8iKu/6vo0ux3pkHHGcoPhFhS+ZomiYeK3 utYjpt2pUeqIdip5vjEFmAk6MLR76cXTu2FbmiXV8OxlgocPQ5mXQVHjr+fY4pZ8kgAw Oa6Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=w1PtUDyr1QUs0IxL1UAXoBeWPOh3W6VBEJ7G4WJMBTE=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=Yz7CmfT1Z8CG3jL1PN9M1CunlezwypnaB1M9FTWoanQNsONXxUb8VaxPDyaA6k3xVu MhKLjCwofa05BfwxP3ZjPfIggquln/SQPmmuD+/cu+7vzpJ7LDaDdiDKT2jxdAJvyWWg o54QP8t+C8Iq7NptHJVp4fzRljenHBTyAA25sxURLuYipRXnnAxckI8WVGDiB6n9QdK6 vbWu9e6fchDXidImfrYsiqmyhBKnhQnQtFTa5ETVmxHWjkif7xcvYah5w8pRUEPVdzyH K1vVeGOPYwNXrcnbFhN70nBiCkqtnfAjzwSJQ8eT9kEhSKoSQ/xO1ODH3m/E4jvxxiT6 pQpw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Az8XX8nz; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id cb5-20020a05622a1f8500b0042548782f0bsi22146054qtb.522.2023.12.16.13.24.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 13:24:13 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Az8XX8nz; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1CDBF3858D28 for ; Sat, 16 Dec 2023 21:24:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 678133858D28 for ; Sat, 16 Dec 2023 21:23:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 678133858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 678133858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702761830; cv=none; b=fQNK18ch+peKwACwUArPVuJOxe/kVo5tS5quWoWZCVEDzU6gJs5Swv40qiohBo2nmehVgZdTnhJRLI6zLBVj1G3RdDUzWnzM1bfkWfixp7XiOdlzE6HWeuYuMlXzPQMcqk97fyDGI0RLy3NnJ9xWjXK783NGPJ05NtNTSXYW9Jw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702761830; c=relaxed/simple; bh=Z6Xu5+8FVIQzucRjzeHCHxvJznRN5t99JcSk4rxC4qY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lstaiofXHf0mS9fj6SxqfHgInldBgdHZON3OJ4cI9OjE+5asOHzv5CppiHPchci6MvwHbYwhXoucFsyBjt/FMOklmkb02UHsUy+dx74MchWaD++jpd978KDTFTXhBs7k+I0ijTpwLBBIHPV6uc80Ur3iufiLPzqS3eVpzaFYyXo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702761828; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=w1PtUDyr1QUs0IxL1UAXoBeWPOh3W6VBEJ7G4WJMBTE=; b=Az8XX8nzYi396oBpEKXEReSomRHPjQojQHqtUfBE7XtWg0RO+rkyQEj9BiHtBIewdfSxph relbOwclLNGY93B26HS0BWfQxTxQVt2DRN7IVH4LYryeB/Dyqme+1TE+WLz7dkNMDtxuKD 8nIy5eBThyDlISJPDt16/FvWb8TJEng= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-400-0-rtL7H4PRSu9Z8XEeXdMQ-1; Sat, 16 Dec 2023 16:23:44 -0500 X-MC-Unique: 0-rtL7H4PRSu9Z8XEeXdMQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3544F185A781 for ; Sat, 16 Dec 2023 21:23:44 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 844051C060B1; Sat, 16 Dec 2023 21:23:43 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] json: fix escaping of object keys Date: Sat, 16 Dec 2023 16:23:41 -0500 Message-Id: <20231216212341.3443227-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785475212868202874 X-GMAIL-MSGID: 1785475212868202874 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-6634-g30d9a3a69841b1. gcc/ChangeLog: * json.cc (print_escaped_json_string): New, taken from string::print. (object::print): Use it for printing keys. (string::print): Move implementation to print_escaped_json_string. (selftest::test_writing_objects): Add a key containing quote, backslash, and control characters. Signed-off-by: David Malcolm --- gcc/json.cc | 94 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/gcc/json.cc b/gcc/json.cc index 90ddd7ab3b15..350917af5df1 100644 --- a/gcc/json.cc +++ b/gcc/json.cc @@ -28,6 +28,52 @@ along with GCC; see the file COPYING3. If not see using namespace json; +/* Print a JSON string to PP, escaping '"', control characters, + and embedded null bytes. + The string is required to be UTF-8 encoded. */ + +static void +print_escaped_json_string (pretty_printer *pp, + const char *utf8_str, + size_t len) +{ + pp_character (pp, '"'); + for (size_t i = 0; i != len; ++i) + { + char ch = utf8_str[i]; + switch (ch) + { + case '"': + pp_string (pp, "\\\""); + break; + case '\\': + pp_string (pp, "\\\\"); + break; + case '\b': + pp_string (pp, "\\b"); + break; + case '\f': + pp_string (pp, "\\f"); + break; + case '\n': + pp_string (pp, "\\n"); + break; + case '\r': + pp_string (pp, "\\r"); + break; + case '\t': + pp_string (pp, "\\t"); + break; + case '\0': + pp_string (pp, "\\0"); + break; + default: + pp_character (pp, ch); + } + } + pp_character (pp, '"'); +} + /* class json::value. */ /* Dump this json::value tree to OUTF. @@ -85,9 +131,7 @@ object::print (pretty_printer *pp, bool formatted) const } map_t &mut_map = const_cast (m_map); value *value = *mut_map.get (key); - pp_doublequote (pp); - pp_string (pp, key); // FIXME: escaping? - pp_doublequote (pp); + print_escaped_json_string (pp, key, strlen (key)); pp_string (pp, ": "); const int indent = strlen (key) + 4; if (formatted) @@ -284,41 +328,7 @@ void string::print (pretty_printer *pp, bool formatted ATTRIBUTE_UNUSED) const { - pp_character (pp, '"'); - for (size_t i = 0; i != m_len; ++i) - { - char ch = m_utf8[i]; - switch (ch) - { - case '"': - pp_string (pp, "\\\""); - break; - case '\\': - pp_string (pp, "\\\\"); - break; - case '\b': - pp_string (pp, "\\b"); - break; - case '\f': - pp_string (pp, "\\f"); - break; - case '\n': - pp_string (pp, "\\n"); - break; - case '\r': - pp_string (pp, "\\r"); - break; - case '\t': - pp_string (pp, "\\t"); - break; - case '\0': - pp_string (pp, "\\0"); - break; - default: - pp_character (pp, ch); - } - } - pp_character (pp, '"'); + print_escaped_json_string (pp, m_utf8, m_len); } /* class json::literal, a subclass of json::value. */ @@ -388,13 +398,17 @@ test_writing_objects () object obj; obj.set_string ("foo", "bar"); obj.set_string ("baz", "quux"); + obj.set_string ("\"\\\b\f\n\r\t", "value for awkward key"); + /* This test relies on json::object writing out key/value pairs in key-insertion order. */ ASSERT_PRINT_EQ (obj, true, "{\"foo\": \"bar\",\n" - " \"baz\": \"quux\"}"); + " \"baz\": \"quux\",\n" + " \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); ASSERT_PRINT_EQ (obj, false, - "{\"foo\": \"bar\", \"baz\": \"quux\"}"); + "{\"foo\": \"bar\", \"baz\": \"quux\"" + ", \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); } /* Verify that JSON arrays are written correctly. */