From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani Newsgroups: gmane.emacs.devel Subject: Re: [PATCH 2/2] JSON serialization: reject duplicate keys in hashtables Date: Sun, 24 Dec 2017 12:53:43 +0000 Message-ID: References: <20171218231730.38840-1-phst@google.com> <20171218231730.38840-2-phst@google.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="94eb2c122b70c9b5950561158a17" X-Trace: blaine.gmane.org 1514119966 27838 195.159.176.226 (24 Dec 2017 12:52:46 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 24 Dec 2017 12:52:46 +0000 (UTC) Cc: Philipp Stephani To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Dec 24 13:52:42 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eT5le-0006hZ-Au for ged-emacs-devel@m.gmane.org; Sun, 24 Dec 2017 13:52:38 +0100 Original-Received: from localhost ([::1]:49153 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eT5nc-00029D-VX for ged-emacs-devel@m.gmane.org; Sun, 24 Dec 2017 07:54:40 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eT5mu-00028m-Hu for emacs-devel@gnu.org; Sun, 24 Dec 2017 07:53:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eT5mt-0005RV-FA for emacs-devel@gnu.org; Sun, 24 Dec 2017 07:53:56 -0500 Original-Received: from mail-qt0-x22b.google.com ([2607:f8b0:400d:c0d::22b]:42020) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eT5mt-0005RH-9R for emacs-devel@gnu.org; Sun, 24 Dec 2017 07:53:55 -0500 Original-Received: by mail-qt0-x22b.google.com with SMTP id g9so40989465qth.9 for ; Sun, 24 Dec 2017 04:53:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=koVMocBrqA1fuBZwKu3w+FpBFltKceuNKQ0BV+3FpWs=; b=dstUFOKOTgPTGBqOFCjio/pQuTD6tflsXchycS2J1hEFADtF8fT8CmOvdU1cDdjQRj LiPhbmdy86crS2ovkdLtOX8i4YuGbQjKLUwySFPRwy2IQXn1KZCue7HSDSAWywXgbVSr qxVFNnse1QlrMx8PBkZFfkkMR/iqBvNd8paxHS4ODj/o5GgRpcD08jm51tc4uYjvLguf +NTb6cYVRea+LUNUIpWgcdP25+I+WUUtrxXJNH63JPKtgKTTiP6DDrhtg5Ojiroh8MlI KldPNMaRHp5+rwOMkXSa95jP6GCbtvZD5IU0slGDuTwvbvKpEYC7B9wo8LJ30eqA7qMS cB4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=koVMocBrqA1fuBZwKu3w+FpBFltKceuNKQ0BV+3FpWs=; b=NEX+lYdcZW3r/SrEPCS3r/uqNaMgzzSrF273zUmxq5+Sp+t5kTl6fByuGExDbl6ALk +AtMvD9FodCo5pQJYIFk6P4Xx7USVADbM2jqm6yo7+vsRUWx/l0vKhkJCN6/1EZI7c57 ZUIV6eCWA4rG/caLnrTBomkTVXcFcRfJ5Q5Jy3oZYZM85VWkGq7Qkn9nvQ06LVJ/S1M6 6dKC+Zwmyv51LbWDT33ZneoE/yARLBkLb5zrr1ZHyZWdVuCWJv1uIZu4jXN72j4ObsC/ 93/ExTb9dGd6exVNiayH4asgSkr1V6JvZFq9qeoQzUqQ3gS/tRliKM2hT/dVhoWEAKq8 IQrA== X-Gm-Message-State: AKGB3mKjclug6XlS/vCIzKDVjJPG0ORXbFgboNpGXJNqgzO3Xegx28ld 2yDY8DvciFnP7WWlfpSF4mOoUsoHoBRz7p6ma515WA== X-Google-Smtp-Source: ACJfBosMx6blMotukNpCkR3EBwoWsfxAxpwd1ivcB4Z7QhywFvy5WWkYZt3n9k/MmQW7dy1ro9NkgPBe1jYPBcjutcU= X-Received: by 10.200.27.76 with SMTP id p12mr28379863qtk.310.1514120034234; Sun, 24 Dec 2017 04:53:54 -0800 (PST) In-Reply-To: <20171218231730.38840-2-phst@google.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::22b X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:221397 Archived-At: --94eb2c122b70c9b5950561158a17 Content-Type: text/plain; charset="UTF-8" Pushed to master as 3455192777459a08a38b0adb311a76202e29f48d. Philipp Stephani schrieb am Di., 19. Dez. 2017 um 00:17 Uhr: > * src/json.c (lisp_to_json_toplevel_1): Reject duplicate keys in > hashtables. > > * test/src/json-tests.el (json-serialize/object-with-duplicate-keys): > Add unit tests. > --- > src/json.c | 7 ++++++- > test/src/json-tests.el | 8 ++++++++ > 2 files changed, 14 insertions(+), 1 deletion(-) > > diff --git a/src/json.c b/src/json.c > index b996cad5a6..3eba5c4310 100644 > --- a/src/json.c > +++ b/src/json.c > @@ -340,7 +340,12 @@ lisp_to_json_toplevel_1 (Lisp_Object lisp, json_t > **json) > /* We can't specify the length, so the string must be > null-terminated. */ > check_string_without_embedded_nulls (key); > - int status = json_object_set_new (*json, SSDATA (key), > + const char *key_data = SSDATA (key); > + /* Reject duplicate keys. These are possible if the hash > + table test is not `equal'. */ > + if (json_object_get (*json, key_data) != NULL) > + wrong_type_argument (Qjson_value_p, lisp); > + int status = json_object_set_new (*json, key_data, > lisp_to_json (HASH_VALUE > (h, i))); > if (status == -1) > /* FIXME: A failure here might also indicate that the > diff --git a/test/src/json-tests.el b/test/src/json-tests.el > index 551f8ac5fe..4edf9d8df0 100644 > --- a/test/src/json-tests.el > +++ b/test/src/json-tests.el > @@ -52,6 +52,14 @@ > (should (equal (json-serialize table) > "{\"abc\":[1,2,true],\"def\":null}")))) > > +(ert-deftest json-serialize/object-with-duplicate-keys () > + (skip-unless (fboundp 'json-serialize)) > + (let ((table (make-hash-table :test #'eq))) > + (puthash (copy-sequence "abc") [1 2 t] table) > + (puthash (copy-sequence "abc") :null table) > + (should (equal (hash-table-count table) 2)) > + (should-error (json-serialize table) :type 'wrong-type-argument))) > + > (ert-deftest json-parse-string/object () > (skip-unless (fboundp 'json-parse-string)) > (let ((actual > -- > 2.15.1 > > --94eb2c122b70c9b5950561158a17 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Pushed to master as 3455192777459a08a38b0adb311a76202e29f4= 8d.

Philipp Stephani <= ;p.stephani2@gmail.com> sch= rieb am Di., 19. Dez. 2017 um 00:17=C2=A0Uhr:
* src/json.c (lisp_to_json_toplevel_1): Reject duplicate keys in=
hashtables.

* test/src/json-tests.el (json-serialize/object-with-duplicate-keys):
Add unit tests.
---
=C2=A0src/json.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| 7 ++++++-=
=C2=A0test/src/json-tests.el | 8 ++++++++
=C2=A02 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/json.c b/src/json.c
index b996cad5a6..3eba5c4310 100644
--- a/src/json.c
+++ b/src/json.c
@@ -340,7 +340,12 @@ lisp_to_json_toplevel_1 (Lisp_Object lisp, json_t **js= on)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* We can't specify the= length, so the string must be
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 null-terminated.=C2= =A0 */
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0check_string_without_embedd= ed_nulls (key);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 int status =3D json_object_set_n= ew (*json, SSDATA (key),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 const char *key_data =3D SSDATA = (key);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Reject duplicate keys.=C2=A0 = These are possible if the hash
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0table test is not `= equal'.=C2=A0 */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (json_object_get (*json, key_= data) !=3D NULL)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 wrong_type_argument (Qjso= n_value_p, lisp);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 int status =3D json_object_set_n= ew (*json, key_data,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0lisp_to_json (HASH_VALUE (h, i)));
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (status =3D=3D -1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/* FIXME: A failure = here might also indicate that the
diff --git a/test/src/json-tests.el b/test/src/json-tests.el
index 551f8ac5fe..4edf9d8df0 100644
--- a/test/src/json-tests.el
+++ b/test/src/json-tests.el
@@ -52,6 +52,14 @@
=C2=A0 =C2=A0 =C2=A0(should (equal (json-serialize table)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "= ;{\"abc\":[1,2,true],\"def\":null}"))))

+(ert-deftest json-serialize/object-with-duplicate-keys ()
+=C2=A0 (skip-unless (fboundp 'json-serialize))
+=C2=A0 (let ((table (make-hash-table :test #'eq)))
+=C2=A0 =C2=A0 (puthash (copy-sequence "abc") [1 2 t] table)
+=C2=A0 =C2=A0 (puthash (copy-sequence "abc") :null table)
+=C2=A0 =C2=A0 (should (equal (hash-table-count table) 2))
+=C2=A0 =C2=A0 (should-error (json-serialize table) :type 'wrong-type-a= rgument)))
+
=C2=A0(ert-deftest json-parse-string/object ()
=C2=A0 =C2=A0(skip-unless (fboundp 'json-parse-string))
=C2=A0 =C2=A0(let ((actual
--
2.15.1

--94eb2c122b70c9b5950561158a17--