dd65c9
From beef22775206d99b06c95c9a015e1b17bf3e767f Mon Sep 17 00:00:00 2001
dd65c9
From: Jan Synacek <jsynacek@redhat.com>
dd65c9
Date: Thu, 23 Nov 2017 10:13:52 +0100
dd65c9
Subject: [PATCH] conf-parse: remove 4K line length limit
dd65c9
dd65c9
Let's use read_line() to solve our long line limitation.
dd65c9
dd65c9
Fixes #3302.
dd65c9
(cherry picked from commit e6dde451a51dc5aaa7f4d98d39b8fe735f73d2af)
dd65c9
dd65c9
Resolves: #1503106
dd65c9
---
23b3cf
 src/shared/conf-parser.c | 50 +++++++++++++++++++++++++++++-----------
dd65c9
 src/shared/utf8.h        |  1 +
dd65c9
 2 files changed, 38 insertions(+), 13 deletions(-)
dd65c9
dd65c9
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
Pablo Greco 48fc63
index 0b1af6c577..73e4d49ea1 100644
dd65c9
--- a/src/shared/conf-parser.c
dd65c9
+++ b/src/shared/conf-parser.c
dd65c9
@@ -28,6 +28,8 @@
dd65c9
 
dd65c9
 #include "conf-parser.h"
dd65c9
 #include "conf-files.h"
dd65c9
+#include "def.h"
dd65c9
+#include "fileio.h"
dd65c9
 #include "util.h"
dd65c9
 #include "macro.h"
dd65c9
 #include "strv.h"
dd65c9
@@ -339,7 +341,7 @@ int config_parse(const char *unit,
dd65c9
         _cleanup_free_ char *section = NULL, *continuation = NULL;
dd65c9
         _cleanup_fclose_ FILE *ours = NULL;
dd65c9
         unsigned line = 0, section_line = 0;
dd65c9
-        bool section_ignored = false;
dd65c9
+        bool section_ignored = false, allow_bom = true;
dd65c9
         int r;
dd65c9
 
dd65c9
         assert(filename);
dd65c9
@@ -359,21 +361,45 @@ int config_parse(const char *unit,
dd65c9
 
dd65c9
         fd_warn_permissions(filename, fileno(f));
dd65c9
 
dd65c9
-        while (!feof(f)) {
dd65c9
-                char l[LINE_MAX], *p, *c = NULL, *e;
dd65c9
+        for (;;) {
dd65c9
+                _cleanup_free_ char *buf = NULL;
dd65c9
+                char *l, *p, *c = NULL, *e;
dd65c9
                 bool escaped = false;
dd65c9
 
dd65c9
-                if (!fgets(l, sizeof(l), f)) {
dd65c9
-                        if (feof(f))
dd65c9
-                                break;
dd65c9
+                r = read_line(f, LONG_LINE_MAX, &buf;;
dd65c9
+                if (r == 0)
dd65c9
+                        break;
dd65c9
+                if (r == -ENOBUFS) {
dd65c9
+                        if (warn)
dd65c9
+                                log_error_errno(r, "%s:%u: Line too long", filename, line);
dd65c9
 
dd65c9
-                        log_error_errno(errno, "Failed to read configuration file '%s': %m", filename);
dd65c9
-                        return -errno;
dd65c9
+                        return r;
dd65c9
+                }
dd65c9
+                if (r < 0) {
dd65c9
+                        if (warn)
dd65c9
+                                log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
dd65c9
+
dd65c9
+                        return r;
dd65c9
                 }
dd65c9
 
dd65c9
-                truncate_nl(l);
dd65c9
+                l = buf;
dd65c9
+                if (allow_bom) {
dd65c9
+                        char *q;
dd65c9
+
dd65c9
+                        q = startswith(buf, UTF8_BYTE_ORDER_MARK);
dd65c9
+                        if (q) {
dd65c9
+                                l = q;
dd65c9
+                                allow_bom = false;
dd65c9
+                        }
dd65c9
+                }
dd65c9
 
dd65c9
                 if (continuation) {
dd65c9
+                        if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
dd65c9
+                                if (warn)
dd65c9
+                                        log_error("%s:%u: Continuation line too long", filename, line);
dd65c9
+                                return -ENOBUFS;
dd65c9
+                        }
dd65c9
+
dd65c9
                         c = strappend(continuation, l);
dd65c9
                         if (!c) {
dd65c9
                                 if (warn)
dd65c9
@@ -381,8 +407,7 @@ int config_parse(const char *unit,
dd65c9
                                 return -ENOMEM;
dd65c9
                         }
dd65c9
 
dd65c9
-                        free(continuation);
dd65c9
-                        continuation = NULL;
dd65c9
+                        continuation = mfree(continuation);
dd65c9
                         p = c;
dd65c9
                 } else
dd65c9
                         p = l;
dd65c9
@@ -428,8 +453,7 @@ int config_parse(const char *unit,
dd65c9
 
dd65c9
                 if (r < 0) {
dd65c9
                         if (warn)
dd65c9
-                                log_warning_errno(r, "Failed to parse file '%s': %m",
dd65c9
-                                                  filename);
dd65c9
+                                log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
dd65c9
                         return r;
dd65c9
                 }
dd65c9
         }
dd65c9
diff --git a/src/shared/utf8.h b/src/shared/utf8.h
Pablo Greco 48fc63
index 77f663438e..d31737061c 100644
dd65c9
--- a/src/shared/utf8.h
dd65c9
+++ b/src/shared/utf8.h
dd65c9
@@ -26,6 +26,7 @@
dd65c9
 #include "macro.h"
dd65c9
 
dd65c9
 #define UTF8_REPLACEMENT_CHARACTER "\xef\xbf\xbd"
dd65c9
+#define UTF8_BYTE_ORDER_MARK "\xef\xbb\xbf"
dd65c9
 
dd65c9
 const char *utf8_is_valid(const char *s) _pure_;
dd65c9
 char *ascii_is_valid(const char *s) _pure_;