aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Stephani2020-06-29 12:32:56 +0200
committerPhilipp Stephani2020-06-29 12:32:56 +0200
commit59e768d64ad97782249fda9e53b6adc94c6d0130 (patch)
tree3221ba10b639751b10a067c1fab8017830ab46f2
parentcce00bef0313bc42beee8096d9312313889dc92d (diff)
downloademacs-59e768d64ad97782249fda9e53b6adc94c6d0130.tar.gz
emacs-59e768d64ad97782249fda9e53b6adc94c6d0130.zip
Fix undefined behavior in json.c (Bug#42113)
* src/json.c (lisp_to_json_toplevel_1, Fjson_parse_string): Check whether input strings are actually strings. * test/src/json-tests.el (json-parse-string/wrong-type) (json-serialize/wrong-hash-key-type): New regression tests.
-rw-r--r--src/json.c2
-rw-r--r--test/src/json-tests.el12
2 files changed, 14 insertions, 0 deletions
diff --git a/src/json.c b/src/json.c
index 2e50ce514fd..4648cb4c3b7 100644
--- a/src/json.c
+++ b/src/json.c
@@ -365,6 +365,7 @@ lisp_to_json_toplevel_1 (Lisp_Object lisp,
365 Lisp_Object key = HASH_KEY (h, i); 365 Lisp_Object key = HASH_KEY (h, i);
366 if (!EQ (key, Qunbound)) 366 if (!EQ (key, Qunbound))
367 { 367 {
368 CHECK_STRING (key);
368 Lisp_Object ekey = json_encode (key); 369 Lisp_Object ekey = json_encode (key);
369 /* We can't specify the length, so the string must be 370 /* We can't specify the length, so the string must be
370 NUL-terminated. */ 371 NUL-terminated. */
@@ -975,6 +976,7 @@ usage: (json-parse-string STRING &rest ARGS) */)
975#endif 976#endif
976 977
977 Lisp_Object string = args[0]; 978 Lisp_Object string = args[0];
979 CHECK_STRING (string);
978 Lisp_Object encoded = json_encode (string); 980 Lisp_Object encoded = json_encode (string);
979 check_string_without_embedded_nuls (encoded); 981 check_string_without_embedded_nuls (encoded);
980 struct json_configuration conf = 982 struct json_configuration conf =
diff --git a/test/src/json-tests.el b/test/src/json-tests.el
index 7eeef885198..028f92f29d3 100644
--- a/test/src/json-tests.el
+++ b/test/src/json-tests.el
@@ -296,5 +296,17 @@ Test with both unibyte and multibyte strings."
296 (1+ most-positive-fixnum) 296 (1+ most-positive-fixnum)
297 (1- most-negative-fixnum))))) 297 (1- most-negative-fixnum)))))
298 298
299(ert-deftest json-parse-string/wrong-type ()
300 "Check that Bug#42113 is fixed."
301 (skip-unless (fboundp 'json-parse-string))
302 (should-error (json-parse-string 1) :type 'wrong-type-argument))
303
304(ert-deftest json-serialize/wrong-hash-key-type ()
305 "Check that Bug#42113 is fixed."
306 (skip-unless (fboundp 'json-serialize))
307 (let ((table (make-hash-table :test #'eq)))
308 (puthash 1 2 table)
309 (should-error (json-serialize table) :type 'wrong-type-argument)))
310
299(provide 'json-tests) 311(provide 'json-tests)
300;;; json-tests.el ends here 312;;; json-tests.el ends here