ff6046
From 452aefc33ab5ebe9c3725d7680ce16399b486a9b Mon Sep 17 00:00:00 2001
ff6046
From: Evgeny Vereshchagin <evvers@ya.ru>
ff6046
Date: Mon, 3 Sep 2018 06:46:24 +0000
ff6046
Subject: [PATCH] tests: rework the code fuzzing journald
ff6046
ff6046
This should make it easier to add a new fuzzer without a lot of
ff6046
duplication.
ff6046
ff6046
(cherry picked from commit b1bd453f36b9428b6bf9feba31fa0a2b36143e9c)
ff6046
ff6046
Resolves: #1696224
ff6046
---
ff6046
 src/fuzz/fuzz-journald-syslog.c | 23 ++---------------------
ff6046
 src/fuzz/fuzz-journald.h        | 30 ++++++++++++++++++++++++++++++
ff6046
 2 files changed, 32 insertions(+), 21 deletions(-)
ff6046
 create mode 100644 src/fuzz/fuzz-journald.h
ff6046
ff6046
diff --git a/src/fuzz/fuzz-journald-syslog.c b/src/fuzz/fuzz-journald-syslog.c
ff6046
index 7730f60875..100f0ce691 100644
ff6046
--- a/src/fuzz/fuzz-journald-syslog.c
ff6046
+++ b/src/fuzz/fuzz-journald-syslog.c
ff6046
@@ -1,29 +1,10 @@
ff6046
 /* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
 
ff6046
-#include "alloc-util.h"
ff6046
 #include "fuzz.h"
ff6046
-#include "journald-server.h"
ff6046
+#include "fuzz-journald.h"
ff6046
 #include "journald-syslog.h"
ff6046
-#include "sd-event.h"
ff6046
 
ff6046
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ff6046
-        Server s = {};
ff6046
-        char *label = NULL;
ff6046
-        size_t label_len = 0;
ff6046
-        struct ucred *ucred = NULL;
ff6046
-        struct timeval *tv = NULL;
ff6046
-
ff6046
-        if (size == 0)
ff6046
-                return 0;
ff6046
-
ff6046
-        assert_se(sd_event_default(&s.event) >= 0);
ff6046
-        s.syslog_fd = s.native_fd = s.stdout_fd = s.dev_kmsg_fd = s.audit_fd = s.hostname_fd = s.notify_fd = -1;
ff6046
-        s.buffer = memdup_suffix0(data, size);
ff6046
-        assert_se(s.buffer);
ff6046
-        s.buffer_size = size + 1;
ff6046
-        s.storage = STORAGE_NONE;
ff6046
-        server_process_syslog_message(&s, s.buffer, size, ucred, tv, label, label_len);
ff6046
-        server_done(&s);
ff6046
-
ff6046
+        fuzz_journald_processing_function(data, size, server_process_syslog_message);
ff6046
         return 0;
ff6046
 }
ff6046
diff --git a/src/fuzz/fuzz-journald.h b/src/fuzz/fuzz-journald.h
ff6046
new file mode 100644
ff6046
index 0000000000..e66ef54c9b
ff6046
--- /dev/null
ff6046
+++ b/src/fuzz/fuzz-journald.h
ff6046
@@ -0,0 +1,30 @@
ff6046
+/* SPDX-License-Identifier: LGPL-2.1+ */
ff6046
+#pragma once
ff6046
+
ff6046
+#include "alloc-util.h"
ff6046
+#include "journald-server.h"
ff6046
+#include "sd-event.h"
ff6046
+
ff6046
+static void fuzz_journald_processing_function(
ff6046
+                const uint8_t *data,
ff6046
+                size_t size,
ff6046
+                void (*f)(Server *s, const char *buf, size_t raw_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len)
ff6046
+        ) {
ff6046
+        Server s = {};
ff6046
+        char *label = NULL;
ff6046
+        size_t label_len = 0;
ff6046
+        struct ucred *ucred = NULL;
ff6046
+        struct timeval *tv = NULL;
ff6046
+
ff6046
+        if (size == 0)
ff6046
+                return;
ff6046
+
ff6046
+        assert_se(sd_event_default(&s.event) >= 0);
ff6046
+        s.syslog_fd = s.native_fd = s.stdout_fd = s.dev_kmsg_fd = s.audit_fd = s.hostname_fd = s.notify_fd = -1;
ff6046
+        s.buffer = memdup_suffix0(data, size);
ff6046
+        assert_se(s.buffer);
ff6046
+        s.buffer_size = size + 1;
ff6046
+        s.storage = STORAGE_NONE;
ff6046
+        (*f)(&s, s.buffer, size, ucred, tv, label, label_len);
ff6046
+        server_done(&s);
ff6046
+}