]> granicus.if.org Git - python/commitdiff
Issue #24683: Fixed crashes in _json functions called with arguments of
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 26 Jul 2015 06:01:22 +0000 (09:01 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 26 Jul 2015 06:01:22 +0000 (09:01 +0300)
inappropriate type.

Lib/test/test_json/test_separators.py
Misc/NEWS
Modules/_json.c

index 84a2be9ae020e78a03e5b6dc81083e851a122763..8ca517405179e510d36a441b230c57919bae60c6 100644 (file)
@@ -39,6 +39,12 @@ class TestSeparators:
         self.assertEqual(h2, h)
         self.assertEqual(d2, expect)
 
+    def test_illegal_separators(self):
+        h = {1: 2, 3: 4}
+        self.assertRaises(TypeError, self.dumps, h, separators=(b', ', ': '))
+        self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': '))
+        self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': '))
+
 
 class TestPySeparators(TestSeparators, PyTest): pass
 class TestCSeparators(TestSeparators, CTest): pass
index a347b940bb2d12e360a98655e1e8fab0ec5a8f65..b84e38d2031d4177504c23ac51d40e251667d27f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #24683: Fixed crashes in _json functions called with arguments of
+  inappropriate type.
+
 - Issue #21697: shutil.copytree() now correctly handles symbolic links that
   point to directories.  Patch by Eduardo Seabra and Thomas Kluyver.
 
index 2f42c3459cbb311f15a3167c3e99b852f0f794f8..dded2c965be57a7c2bd9d1121dd7ca023a3d21a1 100644 (file)
@@ -1223,11 +1223,19 @@ encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
     assert(PyEncoder_Check(self));
     s = (PyEncoderObject *)self;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOp:make_encoder", kwlist,
-        &markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUOOp:make_encoder", kwlist,
+        &markers, &defaultfn, &encoder, &indent,
+        &key_separator, &item_separator,
         &sort_keys, &skipkeys, &allow_nan))
         return -1;
 
+    if (markers != Py_None && !PyDict_Check(markers)) {
+        PyErr_Format(PyExc_TypeError,
+                     "make_encoder() argument 1 must be dict or None, "
+                     "not %.200s", Py_TYPE(markers)->tp_name);
+        return -1;
+    }
+
     s->markers = markers;
     s->defaultfn = defaultfn;
     s->encoder = encoder;