summaryrefslogtreecommitdiff
blob: 7239bca3e238732948cd664060543f3df9f5e363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
commit 870ee48fe109c289033cd0b7543b6f5ea4e6f128
Author: Joakim Hamren <joakim.hamren@gmail.com>
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();
 }