Skip to content

Commit f002225

Browse files
Issue python#24683: Fixed crashes in _json functions called with arguments of
inappropriate type.
2 parents 940d69d + 83236f7 commit f002225

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/test/test_json/test_separators.py

Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Core and Builtins
2828
Library
2929
-------
3030

31+
- Issue #24683: Fixed crashes in _json functions called with arguments of
32+
inappropriate type.
33+
3134
- Issue #21697: shutil.copytree() now correctly handles symbolic links that
3235
point to directories. Patch by Eduardo Seabra and Thomas Kluyver.
3336

Modules/_json.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,11 +1344,19 @@ encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
13441344
assert(PyEncoder_Check(self));
13451345
s = (PyEncoderObject *)self;
13461346

1347-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOOOOOp:make_encoder", kwlist,
1348-
&markers, &defaultfn, &encoder, &indent, &key_separator, &item_separator,
1347+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUOOp:make_encoder", kwlist,
1348+
&markers, &defaultfn, &encoder, &indent,
1349+
&key_separator, &item_separator,
13491350
&sort_keys, &skipkeys, &allow_nan))
13501351
return -1;
13511352

1353+
if (markers != Py_None && !PyDict_Check(markers)) {
1354+
PyErr_Format(PyExc_TypeError,
1355+
"make_encoder() argument 1 must be dict or None, "
1356+
"not %.200s", Py_TYPE(markers)->tp_name);
1357+
return -1;
1358+
}
1359+
13521360
s->markers = markers;
13531361
s->defaultfn = defaultfn;
13541362
s->encoder = encoder;

0 commit comments

Comments
 (0)