From a6aadf4ae0bae185dc4c414d492a4a781c80ffe5 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Wed, 8 Aug 2018 15:06:36 +0900
Subject: [PATCH] journal: fix syslog_parse_identifier()

Fixes #9829.
---
 src/journal/journald-syslog.c     |  6 +++---
 src/journal/test-journal-syslog.c | 10 ++++++++--
 2 files changed, 11 insertions(+), 5 deletions(-)

--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -212,7 +212,7 @@
         e = l;
         l--;
 
-        if (p[l-1] == ']') {
+        if (l > 0 && p[l-1] == ']') {
                 size_t k = l-1;
 
                 for (;;) {
@@ -237,8 +237,8 @@
         if (t)
                 *identifier = t;
 
-        if (strchr(WHITESPACE, p[e]))
-                e++;
+        e += strspn(p + e, WHITESPACE);
+
         *buf = p + e;
         return e;
 }
--- a/src/journal/test-journal-syslog.c
+++ b/src/journal/test-journal-syslog.c
@@ -23,8 +23,8 @@
 #include "macro.h"
 #include "string-util.h"
 
-static void test_syslog_parse_identifier(const char* str,
-                                         const char *ident, const char*pid, int ret) {
+static void test_syslog_parse_identifier(const char *str,
+                                         const char *ident, const char *pid, int ret) {
         const char *buf = str;
         _cleanup_free_ char *ident2 = NULL, *pid2 = NULL;
         int ret2;
@@ -39,7 +39,13 @@
 int main(void) {
         test_syslog_parse_identifier("pidu[111]: xxx", "pidu", "111", 11);
         test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, 6);
+        test_syslog_parse_identifier("pidu:  xxx", "pidu", NULL, 7);
         test_syslog_parse_identifier("pidu xxx", NULL, NULL, 0);
+        test_syslog_parse_identifier(":", "", NULL, 1);
+        test_syslog_parse_identifier(":  ", "", NULL, 3);
+        test_syslog_parse_identifier("pidu:", "pidu", NULL, 5);
+        test_syslog_parse_identifier("pidu: ", "pidu", NULL, 6);
+        test_syslog_parse_identifier("pidu : ", NULL, NULL, 0);
 
         return 0;
 }