From 32e324a283cea97a2c4c2016d10666ce83336dc7 Mon Sep 17 00:00:00 2001
From: Stephen Dolan <mu@netsoc.tcd.ie>
Date: Mon, 3 Sep 2012 17:18:10 +0100
Subject: [PATCH] Fix a memory leak in JV and add more tests

---
 c/jv.c     | 4 +++-
 c/testdata | 8 ++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/c/jv.c b/c/jv.c
index 9a3d308f..80b9b9b8 100644
--- a/c/jv.c
+++ b/c/jv.c
@@ -240,7 +240,9 @@ jv jv_array_get(jv j, int idx) {
 jv jv_array_set(jv j, int idx, jv val) {
   assert(jv_get_kind(j) == JV_KIND_ARRAY);
   // copy/free of val,j coalesced
-  *jvp_array_write(&j.val.complex, idx) = val;
+  jv* slot = jvp_array_write(&j.val.complex, idx);
+  jv_free(*slot);
+  *slot = val;
   return j;
 }
 
diff --git a/c/testdata b/c/testdata
index 70071186..6ddf09fd 100644
--- a/c/testdata
+++ b/c/testdata
@@ -215,6 +215,14 @@ def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x
 {"bar":42}
 {"foo":42, "bar":42}
 
+.foo |= .+1
+{"foo": 42}
+{"foo": 43}
+
+.[0].a |= {"old":., "new":(.+1)}
+[{"a":1,"b":2}]
+[{"a":{"old":1, "new":2},"b":2}]
+
 def inc(x): x |= .+1; inc(.[].a)
 [{"a":1,"b":2},{"a":2,"b":4},{"a":7,"b":8}]
 [{"a":2,"b":2},{"a":3,"b":4},{"a":8,"b":8}]