6f381c
From d8fabe7a6839eeb0d5d0504471f2d18b07545238 Mon Sep 17 00:00:00 2001
6f381c
From: Lennart Poettering <lennart@poettering.net>
6f381c
Date: Tue, 12 May 2020 18:53:35 +0200
6f381c
Subject: [PATCH] journald: rework end of line marker handling to use a field
6f381c
 table
6f381c
6f381c
(cherry picked from commit 549b7379ba404c33fd448d2bca46a57f6529b00b)
6f381c
6f381c
Related: #2029426
6f381c
---
6f381c
 src/journal/journald-stream.c | 29 ++++++++++++++++++++---------
6f381c
 1 file changed, 20 insertions(+), 9 deletions(-)
6f381c
6f381c
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
6f381c
index c8de984335..58752a5a24 100644
6f381c
--- a/src/journal/journald-stream.c
6f381c
+++ b/src/journal/journald-stream.c
6f381c
@@ -54,6 +54,8 @@ typedef enum LineBreak {
6f381c
         LINE_BREAK_NUL,
6f381c
         LINE_BREAK_LINE_MAX,
6f381c
         LINE_BREAK_EOF,
6f381c
+        _LINE_BREAK_MAX,
6f381c
+        _LINE_BREAK_INVALID = -1,
6f381c
 } LineBreak;
6f381c
 
6f381c
 struct StdoutStream {
6f381c
@@ -233,7 +235,11 @@ fail:
6f381c
         return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
6f381c
 }
6f381c
 
6f381c
-static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_break) {
6f381c
+static int stdout_stream_log(
6f381c
+                StdoutStream *s,
6f381c
+                const char *p,
6f381c
+                LineBreak line_break) {
6f381c
+
6f381c
         struct iovec *iovec;
6f381c
         int priority;
6f381c
         char syslog_priority[] = "PRIORITY=\0";
6f381c
@@ -245,6 +251,9 @@ static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_brea
6f381c
         assert(s);
6f381c
         assert(p);
6f381c
 
6f381c
+        assert(line_break >= 0);
6f381c
+        assert(line_break < _LINE_BREAK_MAX);
6f381c
+
6f381c
         if (s->context)
6f381c
                 (void) client_context_maybe_refresh(s->server, s->context, NULL, NULL, 0, NULL, USEC_INFINITY);
6f381c
         else if (pid_is_valid(s->ucred.pid)) {
6f381c
@@ -296,17 +305,19 @@ static int stdout_stream_log(StdoutStream *s, const char *p, LineBreak line_brea
6f381c
                         iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier);
6f381c
         }
6f381c
 
6f381c
-        if (line_break != LINE_BREAK_NEWLINE) {
6f381c
-                const char *c;
6f381c
+        static const char * const line_break_field_table[_LINE_BREAK_MAX] = {
6f381c
+                [LINE_BREAK_NEWLINE]    = NULL, /* Do not add field if traditional newline */
6f381c
+                [LINE_BREAK_NUL]        = "_LINE_BREAK=nul",
6f381c
+                [LINE_BREAK_LINE_MAX]   = "_LINE_BREAK=line-max",
6f381c
+                [LINE_BREAK_EOF]        = "_LINE_BREAK=eof",
6f381c
+        };
6f381c
 
6f381c
-                /* If this log message was generated due to an uncommon line break then mention this in the log
6f381c
-                 * entry */
6f381c
+        const char *c = line_break_field_table[line_break];
6f381c
 
6f381c
-                c =     line_break == LINE_BREAK_NUL ?      "_LINE_BREAK=nul" :
6f381c
-                        line_break == LINE_BREAK_LINE_MAX ? "_LINE_BREAK=line-max" :
6f381c
-                                                            "_LINE_BREAK=eof";
6f381c
+        /* If this log message was generated due to an uncommon line break then mention this in the log
6f381c
+         * entry */
6f381c
+        if (c)
6f381c
                 iovec[n++] = IOVEC_MAKE_STRING(c);
6f381c
-        }
6f381c
 
6f381c
         message = strappend("MESSAGE=", p);
6f381c
         if (message)