]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fuzz-json: optionally allow logging and output
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 9 May 2022 08:41:36 +0000 (10:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 10 May 2022 10:58:48 +0000 (12:58 +0200)
Similarly to other fuzzers… this makes development easier.

src/fuzz/fuzz-json.c

index ad7460c6fd7f965ba1ea1839ec4d9e1fa27857f0..648a6d441d2ab06e27abfa4865d2d0f47b69c368 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "alloc-util.h"
+#include "env-util.h"
 #include "fd-util.h"
 #include "fuzz.h"
 #include "json.h"
@@ -10,18 +11,26 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         size_t out_size;
         _cleanup_fclose_ FILE *f = NULL, *g = NULL;
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
+        int r;
+
+        /* Disable most logging if not running standalone */
+        if (!getenv("SYSTEMD_LOG_LEVEL"))
+                log_set_max_level(LOG_CRIT);
 
         f = data_to_file(data, size);
         assert_se(f);
 
-        if (json_parse_file(f, NULL, 0, &v, NULL, NULL) < 0)
+        r = json_parse_file(f, NULL, 0, &v, NULL, NULL);
+        if (r < 0) {
+                log_debug_errno(r, "failed to parse input: %m");
                 return 0;
+        }
 
-        g = open_memstream_unlocked(&out, &out_size);
-        assert_se(g);
+        if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
+                assert_se(g = open_memstream_unlocked(&out, &out_size));
 
-        json_variant_dump(v, 0, g, NULL);
-        json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g, NULL);
+        json_variant_dump(v, 0, g ?: stdout, NULL);
+        json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g ?: stdout, NULL);
 
         return 0;
 }