]> granicus.if.org Git - jq/commitdiff
Fix a memory leak when inserting into an object with an already-present key
authorStephen Dolan <mu@netsoc.tcd.ie>
Sat, 1 Sep 2012 17:24:17 +0000 (18:24 +0100)
committerStephen Dolan <mu@netsoc.tcd.ie>
Sat, 1 Sep 2012 17:24:17 +0000 (18:24 +0100)
c/jv.c

diff --git a/c/jv.c b/c/jv.c
index 09b600d8df2b04146604f12fd37729d30dddeb67..6f72c9d42f14c136c2f66ccad36ba855a61f5029 100644 (file)
--- a/c/jv.c
+++ b/c/jv.c
@@ -602,6 +602,7 @@ static jv* jvp_object_write(jv_complex* object, jvp_string* key) {
     jvp_string_free_p(key);
   } else {
     slot = jvp_object_add_slot(object, key, bucket);
+    slot->value = jv_null();
   }
   if (slot == 0) {
     jvp_object_rehash(object);
@@ -609,6 +610,7 @@ static jv* jvp_object_write(jv_complex* object, jvp_string* key) {
     assert(!jvp_object_find_slot(object, key, bucket));
     slot = jvp_object_add_slot(object, key, bucket);
     assert(slot);
+    slot->value = jv_null();
   }
   return &slot->value;
 }
@@ -654,7 +656,9 @@ jv jv_object_set(jv object, jv key, jv value) {
   assert(jv_get_kind(object) == JV_KIND_OBJECT);
   assert(jv_get_kind(key) == JV_KIND_STRING);
   // copy/free of object, key, value coalesced
-  *jvp_object_write(&object.val.complex, jvp_string_ptr(&key.val.complex)) = value;
+  jv* slot = jvp_object_write(&object.val.complex, jvp_string_ptr(&key.val.complex));
+  jv_free(*slot);
+  *slot = value;
   return object;
 }