teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0105-fuzz-json-optionally-allow-logging-and-output.patch

594167
From 3087505025b78b80951ab3a5f496eb255f1a9a21 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Mon, 9 May 2022 10:41:36 +0200
594167
Subject: [PATCH] fuzz-json: optionally allow logging and output
594167
MIME-Version: 1.0
594167
Content-Type: text/plain; charset=UTF-8
594167
Content-Transfer-Encoding: 8bit
594167
594167
Similarly to other fuzzers… this makes development easier.
594167
594167
(cherry picked from commit 9ad955ce40e244a52984c68ae2a6b151d918b4a8)
594167
Related: #2087652
594167
---
594167
 src/fuzz/fuzz-json.c | 19 ++++++++++++++-----
594167
 1 file changed, 14 insertions(+), 5 deletions(-)
594167
594167
diff --git a/src/fuzz/fuzz-json.c b/src/fuzz/fuzz-json.c
594167
index ad7460c6fd..648a6d441d 100644
594167
--- a/src/fuzz/fuzz-json.c
594167
+++ b/src/fuzz/fuzz-json.c
594167
@@ -1,6 +1,7 @@
594167
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
 
594167
 #include "alloc-util.h"
594167
+#include "env-util.h"
594167
 #include "fd-util.h"
594167
 #include "fuzz.h"
594167
 #include "json.h"
594167
@@ -10,18 +11,26 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
594167
         size_t out_size;
594167
         _cleanup_fclose_ FILE *f = NULL, *g = NULL;
594167
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
594167
+        int r;
594167
+
594167
+        /* Disable most logging if not running standalone */
594167
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
594167
+                log_set_max_level(LOG_CRIT);
594167
 
594167
         f = data_to_file(data, size);
594167
         assert_se(f);
594167
 
594167
-        if (json_parse_file(f, NULL, 0, &v, NULL, NULL) < 0)
594167
+        r = json_parse_file(f, NULL, 0, &v, NULL, NULL);
594167
+        if (r < 0) {
594167
+                log_debug_errno(r, "failed to parse input: %m");
594167
                 return 0;
594167
+        }
594167
 
594167
-        g = open_memstream_unlocked(&out, &out_size);
594167
-        assert_se(g);
594167
+        if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
594167
+                assert_se(g = open_memstream_unlocked(&out, &out_size));
594167
 
594167
-        json_variant_dump(v, 0, g, NULL);
594167
-        json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g, NULL);
594167
+        json_variant_dump(v, 0, g ?: stdout, NULL);
594167
+        json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g ?: stdout, NULL);
594167
 
594167
         return 0;
594167
 }