]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/json: fix memleak in sort 23335/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 10 May 2022 08:51:43 +0000 (10:51 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 10 May 2022 15:08:37 +0000 (17:08 +0200)
src/shared/json.c
test/fuzz/fuzz-json/leak-sort [new file with mode: 0644]

index d9eda88b12f8df28451ad7bf884dae9330c05fec..bcc109abc24293abfd6e061e5a43027b8675e226 100644 (file)
@@ -4581,7 +4581,7 @@ static int json_cmp_strings(const void *x, const void *y) {
 
 int json_variant_sort(JsonVariant **v) {
         _cleanup_free_ JsonVariant **a = NULL;
-        JsonVariant *n = NULL;
+        _cleanup_(json_variant_unrefp) JsonVariant *n = NULL;
         size_t m;
         int r;
 
@@ -4614,7 +4614,7 @@ int json_variant_sort(JsonVariant **v) {
         if (!n->sorted) /* Check if this worked. This will fail if there are multiple identical keys used. */
                 return -ENOTUNIQ;
 
-        JSON_VARIANT_REPLACE(*v, n);
+        JSON_VARIANT_REPLACE(*v, TAKE_PTR(n));
 
         return 1;
 }
diff --git a/test/fuzz/fuzz-json/leak-sort b/test/fuzz/fuzz-json/leak-sort
new file mode 100644 (file)
index 0000000..f8446db
--- /dev/null
@@ -0,0 +1 @@
+{"":2,"":6,"-":7}
\ No newline at end of file