richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
8d419f
From 51bbb027e93637f5821215ebb067454ad6620190 Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Tue, 10 May 2022 10:51:43 +0200
8d419f
Subject: [PATCH] shared/json: fix memleak in sort
8d419f
8d419f
(cherry picked from commit 99b1145aae682ddd7554c7e3ac5ebf778e88f87d)
8d419f
Related: #2087652
8d419f
---
8d419f
 src/shared/json.c             | 4 ++--
8d419f
 test/fuzz/fuzz-json/leak-sort | 1 +
8d419f
 2 files changed, 3 insertions(+), 2 deletions(-)
8d419f
 create mode 100644 test/fuzz/fuzz-json/leak-sort
8d419f
8d419f
diff --git a/src/shared/json.c b/src/shared/json.c
8d419f
index 06ef556233..6d23bdf4f9 100644
8d419f
--- a/src/shared/json.c
8d419f
+++ b/src/shared/json.c
8d419f
@@ -4581,7 +4581,7 @@ static int json_cmp_strings(const void *x, const void *y) {
8d419f
 
8d419f
 int json_variant_sort(JsonVariant **v) {
8d419f
         _cleanup_free_ JsonVariant **a = NULL;
8d419f
-        JsonVariant *n = NULL;
8d419f
+        _cleanup_(json_variant_unrefp) JsonVariant *n = NULL;
8d419f
         size_t m;
8d419f
         int r;
8d419f
 
8d419f
@@ -4614,7 +4614,7 @@ int json_variant_sort(JsonVariant **v) {
8d419f
         if (!n->sorted) /* Check if this worked. This will fail if there are multiple identical keys used. */
8d419f
                 return -ENOTUNIQ;
8d419f
 
8d419f
-        JSON_VARIANT_REPLACE(*v, n);
8d419f
+        JSON_VARIANT_REPLACE(*v, TAKE_PTR(n));
8d419f
 
8d419f
         return 1;
8d419f
 }
8d419f
diff --git a/test/fuzz/fuzz-json/leak-sort b/test/fuzz/fuzz-json/leak-sort
8d419f
new file mode 100644
8d419f
index 0000000000..f8446dbdc7
8d419f
--- /dev/null
8d419f
+++ b/test/fuzz/fuzz-json/leak-sort
8d419f
@@ -0,0 +1 @@
8d419f
+{"":2,"":6,"-":7}
8d419f
\ No newline at end of file