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