unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: [PATCH 2/2] JSON serialization: reject duplicate keys in hashtables
Date: Tue, 19 Dec 2017 00:17:30 +0100	[thread overview]
Message-ID: <20171218231730.38840-2-phst@google.com> (raw)
In-Reply-To: <20171218231730.38840-1-phst@google.com>

* 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




  reply	other threads:[~2017-12-18 23:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-18 23:17 [PATCH 1/2] Use Jansson's error code support if available Philipp Stephani
2017-12-18 23:17 ` Philipp Stephani [this message]
2017-12-24 12:53   ` [PATCH 2/2] JSON serialization: reject duplicate keys in hashtables Philipp Stephani
2017-12-19 17:11 ` [PATCH 1/2] Use Jansson's error code support if available Eli Zaretskii
2017-12-19 17:35   ` Philipp Stephani
2017-12-19 19:54     ` Eli Zaretskii
2017-12-19 21:49       ` Philipp Stephani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171218231730.38840-2-phst@google.com \
    --to=p.stephani2@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=phst@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).