All of 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

* Re: [Buildroot] [PATCH] package/json-c: fix memory leak
  2026-11-30 10:01 [Buildroot] [PATCH] package/json-c: fix memory leak Michael Fischer via buildroot
@ 2026-11-30 14:02 ` Yegor Yefremov
  0 siblings, 0 replies; 2+ messages in thread
From: Yegor Yefremov @ 2026-11-30 14:02 UTC (permalink / raw)
  To: Michael Fischer; +Cc: buildroot

Hi Michael,

On Mon, Nov 30, 2026 at 11:02 AM Michael Fischer <mf@go-sys.de> wrote:
>
> 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

(Review done with Claude's help, checked by me.)

Good catch, the analysis is right: Buildroot's CMake toolchain file sets
CMAKE_C_FLAGS_RELEASE to -DNDEBUG, so the free is compiled out.

This has already been fixed upstream, three days after the 0.19 release,
by commit f291fa81c6f2 ("free objects outside assert() in
json_object_put", PR #936), which does the same as your patch and applies
cleanly to 0.19. Could you send a v2 that backports it instead?

- Use the upstream commit as is
  (https://github.com/json-c/json-c/commit/f291fa81c6f21d925ea771a8cca597f5886309cc.patch)
  and add above your Signed-off-by:
  Upstream: https://github.com/json-c/json-c/commit/f291fa81c6f21d925ea771a8cca597f5886309cc
  check-package currently complains about the missing Upstream: tag.

- In the commit message, please add the culprit commit:
  Fixes: b4b0e7b369761a4cea9809f73c836afc8adc3432 ("package/json-c: bump to version 0.19")
  and drop the "add patch: ..." line.

0.18 didn't have this code, and all maintenance branches are still on
0.18, so only 2026.11-rc is affected. It would be good to have this in
before 2026.11 is released.

Best regards,
Yegor
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply	[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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.