From 9adbfeb38ac101d6f73a033bb120d63513ffb240 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 6 Nov 2018 13:22:09 +0900 Subject: [PATCH 1/2] conf-parser: ignore trailing back-slash in comment Fixes #10598. --- src/shared/conf-parser.c | 5 ++++- src/test/test-conf-parser.c | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index b9c5b412f2..493ece9d09 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -174,7 +174,7 @@ static int parse_line( if (!*l) return 0; - if (strchr(COMMENTS "\n", *l)) + if (*l == '\n') return 0; include = first_word(l, ".include"); @@ -327,6 +327,9 @@ int config_parse(const char *unit, return r; } + if (strchr(COMMENTS, *buf)) + continue; + l = buf; if (!(flags & CONFIG_PARSE_REFUSE_BOM)) { char *q; diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index 91f1d9a386..368e02cb33 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -274,6 +274,18 @@ static const char* const config_file[] = { "2\\\n" "3\n", + "[Section]\n" + "#hogehoge\\\n" /* continuation is ignored in comment */ + "setting1=1\\\n" /* normal continuation */ + "2\\\n" + "3\n", + + "[Section]\n" + "setting1=1\\\n" /* normal continuation */ + "#hogehoge\\\n" /* commented out line in continuation is ignored */ + "2\\\n" + "3\n", + "[Section]\n" "setting1=1\\\n" /* continuation with extra trailing backslash at the end */ "2\\\n" @@ -350,27 +362,27 @@ static void test_config_parse(unsigned i, const char *s) { assert_se(streq(setting1, "1")); break; - case 4 ... 5: + case 4 ... 7: assert_se(r == 0); assert_se(streq(setting1, "1 2 3")); break; - case 6: + case 8: assert_se(r == 0); assert_se(streq(setting1, "1\\\\ \\\\2")); break; - case 7: + case 9: assert_se(r == 0); assert_se(streq(setting1, x1000("ABCD"))); break; - case 8 ... 9: + case 10 ... 11: assert_se(r == 0); assert_se(streq(setting1, x1000("ABCD") " foobar")); break; - case 10 ... 11: + case 12 ... 13: assert_se(r == -ENOBUFS); assert_se(setting1 == NULL); break; From 2ca4d779e021fdd94f4445980baa0aa8af6ffdc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 8 Nov 2018 09:36:52 +0100 Subject: [PATCH 2/2] man: document the details of continuations and comments --- man/systemd.syntax.xml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/man/systemd.syntax.xml b/man/systemd.syntax.xml index 3c67e92dd5..67cd640eea 100644 --- a/man/systemd.syntax.xml +++ b/man/systemd.syntax.xml @@ -73,7 +73,8 @@ backslash is replaced by a space character. This may be used to wrap long lines. The limit on line length is very large (currently 1 MB), but it is recommended to avoid such long lines and use multiple directives, variable substitution, or other mechanism as appropriate for the given - file type. + file type. When a comment line or lines follow a line ending with a backslash, the comment block + is ignored, so the continued line is concatenated with whatever follows the comment block. [Section A] KeyOne=value 1 @@ -85,6 +86,12 @@ KeyTwo=value 2 Setting="something" "some thing" "…" KeyTwo=value 2 \ value 2 continued + +[Section C] +KeyThree=value 2\ +# this line is ignored +; this line is ignored too + value 2 continued Boolean arguments used in configuration files can be written in