Skip to content
Snippets Groups Projects
Commit 262a72f7c250 authored by Bob Ippolito's avatar Bob Ippolito
Browse files

encoder_listencode_obj recursion check


Signed-off-by: default avatarBob Ippolito <bob@redivi.com>
parent aedcb3de2d11
Branches
No related tags found
No related merge requests found
...@@ -2145,6 +2145,5 @@ ...@@ -2145,6 +2145,5 @@
encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level) encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssize_t indent_level)
{ {
/* Encode Python object obj to a JSON term, rval is a PyList */ /* Encode Python object obj to a JSON term, rval is a PyList */
PyObject *newobj; int rv = -1;
int rv;
...@@ -2150,3 +2149,4 @@ ...@@ -2150,3 +2149,4 @@
do {
if (obj == Py_None || obj == Py_True || obj == Py_False) { if (obj == Py_None || obj == Py_True || obj == Py_False) {
PyObject *cstr = _encoded_const(obj); PyObject *cstr = _encoded_const(obj);
...@@ -2151,9 +2151,8 @@ ...@@ -2151,9 +2151,8 @@
if (obj == Py_None || obj == Py_True || obj == Py_False) { if (obj == Py_None || obj == Py_True || obj == Py_False) {
PyObject *cstr = _encoded_const(obj); PyObject *cstr = _encoded_const(obj);
if (cstr == NULL) if (cstr != NULL)
return -1; rv = _steal_list_append(rval, cstr);
return _steal_list_append(rval, cstr);
} }
else if (PyString_Check(obj) || PyUnicode_Check(obj)) else if (PyString_Check(obj) || PyUnicode_Check(obj))
{ {
PyObject *encoded = encoder_encode_string(s, obj); PyObject *encoded = encoder_encode_string(s, obj);
...@@ -2156,10 +2155,9 @@ ...@@ -2156,10 +2155,9 @@
} }
else if (PyString_Check(obj) || PyUnicode_Check(obj)) else if (PyString_Check(obj) || PyUnicode_Check(obj))
{ {
PyObject *encoded = encoder_encode_string(s, obj); PyObject *encoded = encoder_encode_string(s, obj);
if (encoded == NULL) if (encoded != NULL)
return -1; rv = _steal_list_append(rval, encoded);
return _steal_list_append(rval, encoded);
} }
else if (PyInt_Check(obj) || PyLong_Check(obj)) { else if (PyInt_Check(obj) || PyLong_Check(obj)) {
PyObject *encoded = PyObject_Str(obj); PyObject *encoded = PyObject_Str(obj);
...@@ -2163,9 +2161,8 @@ ...@@ -2163,9 +2161,8 @@
} }
else if (PyInt_Check(obj) || PyLong_Check(obj)) { else if (PyInt_Check(obj) || PyLong_Check(obj)) {
PyObject *encoded = PyObject_Str(obj); PyObject *encoded = PyObject_Str(obj);
if (encoded == NULL) if (encoded != NULL)
return -1; rv = _steal_list_append(rval, encoded);
return _steal_list_append(rval, encoded);
} }
else if (PyFloat_Check(obj)) { else if (PyFloat_Check(obj)) {
PyObject *encoded = encoder_encode_float(s, obj); PyObject *encoded = encoder_encode_float(s, obj);
...@@ -2169,8 +2166,7 @@ ...@@ -2169,8 +2166,7 @@
} }
else if (PyFloat_Check(obj)) { else if (PyFloat_Check(obj)) {
PyObject *encoded = encoder_encode_float(s, obj); PyObject *encoded = encoder_encode_float(s, obj);
if (encoded == NULL) if (encoded != NULL)
return -1; rv = _steal_list_append(rval, encoded);
return _steal_list_append(rval, encoded);
} }
else if (PyList_Check(obj) || PyTuple_Check(obj)) { else if (PyList_Check(obj) || PyTuple_Check(obj)) {
...@@ -2175,5 +2171,5 @@ ...@@ -2175,5 +2171,5 @@
} }
else if (PyList_Check(obj) || PyTuple_Check(obj)) { else if (PyList_Check(obj) || PyTuple_Check(obj)) {
return encoder_listencode_list(s, rval, obj, indent_level); rv = encoder_listencode_list(s, rval, obj, indent_level);
} }
else if (PyDict_Check(obj)) { else if (PyDict_Check(obj)) {
...@@ -2178,6 +2174,6 @@ ...@@ -2178,6 +2174,6 @@
} }
else if (PyDict_Check(obj)) { else if (PyDict_Check(obj)) {
return encoder_listencode_dict(s, rval, obj, indent_level); rv = encoder_listencode_dict(s, rval, obj, indent_level);
} }
else if (s->use_decimal && Decimal_Check(obj)) { else if (s->use_decimal && Decimal_Check(obj)) {
PyObject *encoded = PyObject_Str(obj); PyObject *encoded = PyObject_Str(obj);
...@@ -2181,9 +2177,8 @@ ...@@ -2181,9 +2177,8 @@
} }
else if (s->use_decimal && Decimal_Check(obj)) { else if (s->use_decimal && Decimal_Check(obj)) {
PyObject *encoded = PyObject_Str(obj); PyObject *encoded = PyObject_Str(obj);
if (encoded == NULL) if (encoded != NULL)
return -1; rv = _steal_list_append(rval, encoded);
return _steal_list_append(rval, encoded);
} }
else { else {
PyObject *ident = NULL; PyObject *ident = NULL;
...@@ -2187,7 +2182,8 @@ ...@@ -2187,7 +2182,8 @@
} }
else { else {
PyObject *ident = NULL; PyObject *ident = NULL;
PyObject *newobj;
if (s->markers != Py_None) { if (s->markers != Py_None) {
int has_key; int has_key;
ident = PyLong_FromVoidPtr(obj); ident = PyLong_FromVoidPtr(obj);
if (ident == NULL) if (ident == NULL)
...@@ -2190,10 +2186,10 @@ ...@@ -2190,10 +2186,10 @@
if (s->markers != Py_None) { if (s->markers != Py_None) {
int has_key; int has_key;
ident = PyLong_FromVoidPtr(obj); ident = PyLong_FromVoidPtr(obj);
if (ident == NULL) if (ident == NULL)
return -1; break;
has_key = PyDict_Contains(s->markers, ident); has_key = PyDict_Contains(s->markers, ident);
if (has_key) { if (has_key) {
if (has_key != -1) if (has_key != -1)
PyErr_SetString(PyExc_ValueError, "Circular reference detected"); PyErr_SetString(PyExc_ValueError, "Circular reference detected");
Py_DECREF(ident); Py_DECREF(ident);
...@@ -2195,9 +2191,9 @@ ...@@ -2195,9 +2191,9 @@
has_key = PyDict_Contains(s->markers, ident); has_key = PyDict_Contains(s->markers, ident);
if (has_key) { if (has_key) {
if (has_key != -1) if (has_key != -1)
PyErr_SetString(PyExc_ValueError, "Circular reference detected"); PyErr_SetString(PyExc_ValueError, "Circular reference detected");
Py_DECREF(ident); Py_DECREF(ident);
return -1; break;
} }
if (PyDict_SetItem(s->markers, ident, obj)) { if (PyDict_SetItem(s->markers, ident, obj)) {
Py_DECREF(ident); Py_DECREF(ident);
...@@ -2201,9 +2197,9 @@ ...@@ -2201,9 +2197,9 @@
} }
if (PyDict_SetItem(s->markers, ident, obj)) { if (PyDict_SetItem(s->markers, ident, obj)) {
Py_DECREF(ident); Py_DECREF(ident);
return -1; break;
} }
} }
newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL); newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
if (newobj == NULL) { if (newobj == NULL) {
Py_XDECREF(ident); Py_XDECREF(ident);
...@@ -2205,11 +2201,11 @@ ...@@ -2205,11 +2201,11 @@
} }
} }
newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL); newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
if (newobj == NULL) { if (newobj == NULL) {
Py_XDECREF(ident); Py_XDECREF(ident);
return -1; break;
} }
rv = encoder_listencode_obj(s, rval, newobj, indent_level); rv = encoder_listencode_obj(s, rval, newobj, indent_level);
Py_DECREF(newobj); Py_DECREF(newobj);
if (rv) { if (rv) {
Py_XDECREF(ident); Py_XDECREF(ident);
...@@ -2211,7 +2207,7 @@ ...@@ -2211,7 +2207,7 @@
} }
rv = encoder_listencode_obj(s, rval, newobj, indent_level); rv = encoder_listencode_obj(s, rval, newobj, indent_level);
Py_DECREF(newobj); Py_DECREF(newobj);
if (rv) { if (rv) {
Py_XDECREF(ident); Py_XDECREF(ident);
return -1; rv = -1;
} }
...@@ -2217,4 +2213,4 @@ ...@@ -2217,4 +2213,4 @@
} }
if (ident != NULL) { else if (ident != NULL) {
if (PyDict_DelItem(s->markers, ident)) { if (PyDict_DelItem(s->markers, ident)) {
Py_XDECREF(ident); Py_XDECREF(ident);
...@@ -2219,6 +2215,6 @@ ...@@ -2219,6 +2215,6 @@
if (PyDict_DelItem(s->markers, ident)) { if (PyDict_DelItem(s->markers, ident)) {
Py_XDECREF(ident); Py_XDECREF(ident);
return -1; rv = -1;
} }
Py_XDECREF(ident); Py_XDECREF(ident);
} }
...@@ -2222,5 +2218,8 @@ ...@@ -2222,5 +2218,8 @@
} }
Py_XDECREF(ident); Py_XDECREF(ident);
} }
}
} while (0);
Py_LeaveRecursiveCall();
return rv; return rv;
} }
...@@ -2225,6 +2224,5 @@ ...@@ -2225,6 +2224,5 @@
return rv; return rv;
} }
}
static int static int
encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level) encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ssize_t indent_level)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment