Skip to content

Commit 83236f7

Browse files
Issue #24683: Fixed crashes in _json functions called with arguments of
inappropriate type.
1 parent 5a294d8 commit 83236f7

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/test/test_json/test_separators.py

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def test_separators(self):
3939
self.assertEqual(h2, h)
4040
self.assertEqual(d2, expect)
4141

42+
def test_illegal_separators(self):
43+
h = {1: 2, 3: 4}
44+
self.assertRaises(TypeError, self.dumps, h, separators=(b', ', ': '))
45+
self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': '))
46+
self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': '))
47+
4248

4349
class TestPySeparators(TestSeparators, PyTest): pass
4450
class TestCSeparators(TestSeparators, CTest): pass

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #24683: Fixed crashes in _json functions called with arguments of
70+
inappropriate type.
71+
6972
- Issue #21697: shutil.copytree() now correctly handles symbolic links that
7073
point to directories. Patch by Eduardo Seabra and Thomas Kluyver.
7174

Modules/_json.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,19 @@ encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
12231223
assert(PyEncoder_Check(self));
12241224
s = (PyEncoderObject *)self;
12251225

1226-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOp:make_encoder", kwlist,
1227-
&markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1226+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUOOp:make_encoder", kwlist,
1227+
&markers, &defaultfn, &encoder, &indent,
1228+
&key_separator, &item_separator,
12281229
&sort_keys, &skipkeys, &allow_nan))
12291230
return -1;
12301231

1232+
if (markers != Py_None && !PyDict_Check(markers)) {
1233+
PyErr_Format(PyExc_TypeError,
1234+
"make_encoder() argument 1 must be dict or None, "
1235+
"not %.200s", Py_TYPE(markers)->tp_name);
1236+
return -1;
1237+
}
1238+
12311239
s->markers = markers;
12321240
s->defaultfn = defaultfn;
12331241
s->encoder = encoder;

0 commit comments

Comments
 (0)