ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
36e8a3
From d355618518f26bd045df81a52dade79ac3079f3f Mon Sep 17 00:00:00 2001
36e8a3
From: Yu Watanabe <watanabe.yu+github@gmail.com>
36e8a3
Date: Wed, 8 Aug 2018 15:06:36 +0900
36e8a3
Subject: [PATCH] journal: fix syslog_parse_identifier()
36e8a3
36e8a3
Fixes #9829.
36e8a3
36e8a3
(cherry-picked from commit a6aadf4ae0bae185dc4c414d492a4a781c80ffe5)
36e8a3
36e8a3
Resolves: #1664978
36e8a3
---
36e8a3
 src/journal/journald-syslog.c     |  6 +++---
36e8a3
 src/journal/test-journal-syslog.c | 10 ++++++++--
36e8a3
 2 files changed, 11 insertions(+), 5 deletions(-)
36e8a3
36e8a3
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
4bff0a
index 9dea116722..97711ac7a3 100644
36e8a3
--- a/src/journal/journald-syslog.c
36e8a3
+++ b/src/journal/journald-syslog.c
36e8a3
@@ -194,7 +194,7 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid)
36e8a3
         e = l;
36e8a3
         l--;
36e8a3
 
36e8a3
-        if (p[l-1] == ']') {
36e8a3
+        if (l > 0 && p[l-1] == ']') {
36e8a3
                 size_t k = l-1;
36e8a3
 
36e8a3
                 for (;;) {
36e8a3
@@ -219,8 +219,8 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid)
36e8a3
         if (t)
36e8a3
                 *identifier = t;
36e8a3
 
36e8a3
-        if (strchr(WHITESPACE, p[e]))
36e8a3
-                e++;
36e8a3
+        e += strspn(p + e, WHITESPACE);
36e8a3
+
36e8a3
         *buf = p + e;
36e8a3
         return e;
36e8a3
 }
36e8a3
diff --git a/src/journal/test-journal-syslog.c b/src/journal/test-journal-syslog.c
4bff0a
index 9ba86f6c8a..05f759817e 100644
36e8a3
--- a/src/journal/test-journal-syslog.c
36e8a3
+++ b/src/journal/test-journal-syslog.c
36e8a3
@@ -5,8 +5,8 @@
36e8a3
 #include "macro.h"
36e8a3
 #include "string-util.h"
36e8a3
 
36e8a3
-static void test_syslog_parse_identifier(const char* str,
36e8a3
-                                         const char *ident, const char*pid, int ret) {
36e8a3
+static void test_syslog_parse_identifier(const char *str,
36e8a3
+                                         const char *ident, const char *pid, int ret) {
36e8a3
         const char *buf = str;
36e8a3
         _cleanup_free_ char *ident2 = NULL, *pid2 = NULL;
36e8a3
         int ret2;
36e8a3
@@ -21,7 +21,13 @@ static void test_syslog_parse_identifier(const char* str,
36e8a3
 int main(void) {
36e8a3
         test_syslog_parse_identifier("pidu[111]: xxx", "pidu", "111", 11);
36e8a3
         test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, 6);
36e8a3
+        test_syslog_parse_identifier("pidu:  xxx", "pidu", NULL, 7);
36e8a3
         test_syslog_parse_identifier("pidu xxx", NULL, NULL, 0);
36e8a3
+        test_syslog_parse_identifier(":", "", NULL, 1);
36e8a3
+        test_syslog_parse_identifier(":  ", "", NULL, 3);
36e8a3
+        test_syslog_parse_identifier("pidu:", "pidu", NULL, 5);
36e8a3
+        test_syslog_parse_identifier("pidu: ", "pidu", NULL, 6);
36e8a3
+        test_syslog_parse_identifier("pidu : ", NULL, NULL, 0);
36e8a3
 
36e8a3
         return 0;
36e8a3
 }