Avoid problems with object's serial number growing too big
authorPetri Lehtinen <petri@digip.org>
Mon, 14 Nov 2011 17:10:28 +0000 (19:10 +0200)
committerPetri Lehtinen <petri@digip.org>
Mon, 14 Nov 2011 19:01:13 +0000 (21:01 +0200)
Transform serial key comparison from substraction to real comparison.
Reset serial to zero in json_object_clear() to avoid it growing out of
bounds when reusing objects.

Closes GH-40.
Closes GH-41.

src/dump.c
src/value.c

index 33112ba..089474d 100644 (file)
@@ -159,8 +159,10 @@ static int object_key_compare_keys(const void *key1, const void *key2)
 
 static int object_key_compare_serials(const void *key1, const void *key2)
 {
 
 static int object_key_compare_serials(const void *key1, const void *key2)
 {
-    return (*(const object_key_t **)key1)->serial -
-           (*(const object_key_t **)key2)->serial;
+    size_t a = (*(const object_key_t **)key1)->serial;
+    size_t b = (*(const object_key_t **)key2)->serial;
+
+    return a < b ? -1 : a == b ? 0 : 1;
 }
 
 static int do_dump(const json_t *json, size_t flags, int depth,
 }
 
 static int do_dump(const json_t *json, size_t flags, int depth,
index d0517d5..5ef4138 100644 (file)
@@ -186,7 +186,9 @@ int json_object_clear(json_t *json)
         return -1;
 
     object = json_to_object(json);
         return -1;
 
     object = json_to_object(json);
+
     hashtable_clear(&object->hashtable);
     hashtable_clear(&object->hashtable);
+    object->serial = 0;
 
     return 0;
 }
 
     return 0;
 }