summaryrefslogtreecommitdiff
path: root/simplejson/_speedups.c
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2021-08-23 21:13:32 -0700
committerGitHub <noreply@github.com>2021-08-23 21:13:32 -0700
commit8dce5b06d3faf9cc439a56dfa3cbdcde2dd8c8ec (patch)
tree38d79d362e5ff641022a9a3fd7e7f2e0b0bee8d1 /simplejson/_speedups.c
parent5a40364f951f85ed73d6aaf881f8ee9521a56f07 (diff)
parent6eeec11fcebf80c314e3a408897ac6c811153f24 (diff)
downloadsimplejson-8dce5b06d3faf9cc439a56dfa3cbdcde2dd8c8ec.tar.gz
Merge pull request #284 from gpshead/fix_is_namedtuple_dict_fu
Fix the C extension module to harden is_namedtuple.
Diffstat (limited to 'simplejson/_speedups.c')
-rw-r--r--simplejson/_speedups.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
index e710128..6fefa99 100644
--- a/simplejson/_speedups.c
+++ b/simplejson/_speedups.c
@@ -386,6 +386,8 @@ static int
_is_namedtuple(PyObject *obj)
{
int rval = 0;
+ /* We intentionally accept anything with a duck typed _asdict method rather
+ * than requiring it to pass PyTuple_Check(obj). */
PyObject *_asdict = PyObject_GetAttrString(obj, "_asdict");
if (_asdict == NULL) {
PyErr_Clear();
@@ -2853,6 +2855,15 @@ encoder_listencode_obj(PyEncoderObject *s, JSON_Accu *rval, PyObject *obj, Py_ss
return rv;
newobj = PyObject_CallMethod(obj, "_asdict", NULL);
if (newobj != NULL) {
+ if (!PyDict_Check(newobj)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "_asdict() must return a dict, not %.80s",
+ Py_TYPE(newobj)->tp_name
+ );
+ Py_DECREF(newobj);
+ return -1;
+ }
rv = encoder_listencode_dict(s, rval, newobj, indent_level);
Py_DECREF(newobj);
}