commit 870ee48fe109c289033cd0b7543b6f5ea4e6f128 Author: Joakim Hamren Date: Sat Feb 4 01:07:52 2017 +0100 Fixes for sort_keys bug and a typo. - Fixed segfault when using sort_keys=True on dict with unorderable keys (GH247) - Fixed refcount becoming negative when using sort_keys=True (GH243) - Fixed compile error when defining JSON_NO_EXTRA_WHITESPACE caused by a wrongly named variable. (GH245) diff --git a/lib/ultrajsonenc.c b/lib/ultrajsonenc.c index cb10024..1bfa8f4 100644 --- a/lib/ultrajsonenc.c +++ b/lib/ultrajsonenc.c @@ -717,7 +717,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c { const char *value; char *objName; - int count; + int count, res; JSOBJ iterObj; size_t szlen; JSONTypeContext tc; @@ -796,7 +796,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c { Buffer_AppendCharUnchecked (enc, ','); #ifndef JSON_NO_EXTRA_WHITESPACE - Buffer_AppendCharUnchecked (buffer, ' '); + Buffer_AppendCharUnchecked (enc, ' '); #endif Buffer_AppendIndentNewlineUnchecked (enc); } @@ -823,8 +823,16 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c Buffer_AppendCharUnchecked (enc, '{'); Buffer_AppendIndentNewlineUnchecked (enc); - while (enc->iterNext(obj, &tc)) + while ((res = enc->iterNext(obj, &tc))) { + if(res < 0) + { + enc->iterEnd(obj, &tc); + enc->endTypeContext(obj, &tc); + enc->level--; + return; + } + if (count > 0) { Buffer_AppendCharUnchecked (enc, ','); diff --git a/python/objToJSON.c b/python/objToJSON.c index 1960d40..8133fb5 100644 --- a/python/objToJSON.c +++ b/python/objToJSON.c @@ -537,6 +537,7 @@ static int SortedDict_iterNext(JSOBJ obj, JSONTypeContext *tc) // Sort the list. if (PyList_Sort(items) < 0) { + PyErr_SetString(PyExc_ValueError, "unorderable keys"); goto error; } @@ -607,7 +608,6 @@ static void SortedDict_iterEnd(JSOBJ obj, JSONTypeContext *tc) { GET_TC(tc)->itemName = NULL; GET_TC(tc)->itemValue = NULL; - Py_DECREF(GET_TC(tc)->newObj); Py_DECREF(GET_TC(tc)->dictObj); PRINTMARK(); }