Buildroot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [Buildroot] [PATCH] package/json-c: fix memory leak
@ 2026-11-30 10:01 Michael Fischer via buildroot
  2026-11-30 14:02 ` Yegor Yefremov
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Fischer via buildroot @ 2026-11-30 10:01 UTC (permalink / raw)
  To: buildroot; +Cc: Michael Fischer

json-c 0.19 rewrote json_object_put() iteratively (issue #923). The final
release of every non-empty object/array happens inside an assert():

    assert(_json_object_put_maybe_free(jso, 1) == 0);

Buildroot builds json-c as CMAKE_BUILD_TYPE=Release, i.e. with -DNDEBUG, so
the expression is never evaluated and every non-empty container leaks
(measured on a BlueBox: ~0.5 kB per JSON object, the web interface alone
filled the heap until the memory watchdog restarted the device).

Evaluate the call unconditionally and only assert on its result.

add patch: 0001-json_object_put-do-not-free-inside-assert.patch

Signed-off-by: Michael Fischer <mf@go-sys.de>
---
 ...object_put-do-not-free-inside-assert.patch | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 package/json-c/0001-json_object_put-do-not-free-inside-assert.patch

diff --git a/package/json-c/0001-json_object_put-do-not-free-inside-assert.patch b/package/json-c/0001-json_object_put-do-not-free-inside-assert.patch
new file mode 100644
index 0000000000..807d4cf589
--- /dev/null
+++ b/package/json-c/0001-json_object_put-do-not-free-inside-assert.patch
@@ -0,0 +1,37 @@
+From: Michael Fischer <mf@go-sys.de>
+Date: Wed, 11 Nov 2026 15:00:00 +0100
+Subject: [PATCH] json_object_put: do not free containers inside assert()
+
+json-c 0.19 rewrote json_object_put() iteratively (issue #923). The final
+release of every non-empty object/array happens inside an assert():
+
+    assert(_json_object_put_maybe_free(jso, 1) == 0);
+
+Buildroot builds json-c as CMAKE_BUILD_TYPE=Release, i.e. with -DNDEBUG, so
+the expression is never evaluated and every non-empty container leaks
+(measured on a BlueBox: ~0.5 kB per JSON object, the web interface alone
+filled the heap until the memory watchdog restarted the device).
+
+Evaluate the call unconditionally and only assert on its result.
+
+Signed-off-by: Michael Fischer <mf@go-sys.de>
+---
+ json_object.c | 6 +++++-
+ 1 file changed, 5 insertions(+), 1 deletion(-)
+
+diff --git a/json_object.c b/json_object.c
+--- a/json_object.c
++++ b/json_object.c
+@@ -439,7 +439,11 @@ int json_object_put(struct json_object *jso)
+ 			// so we need to actually free it now
+ 			assert(jso->_ref_count == 0);
+ 			jso->_ref_count++;   // We're the exclusive owner of jso, non-atomic add is ok.
+-			assert(_json_object_put_maybe_free(jso, 1) == 0);
++			{
++				int freed = _json_object_put_maybe_free(jso, 1);
++				assert(freed == 0);
++				(void)freed;
++			}
+ 			jso = parent;
+ 			// iteration will be reset at the top of the loop
+ 		}
-- 
2.53.0

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 14:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:01 [Buildroot] [PATCH] package/json-c: fix memory leak Michael Fischer via buildroot
2026-11-30 14:02 ` Yegor Yefremov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox