Merge pull request #10651 from yuwata/fix-10598

conf-parser: ignore trailing back-slash in comment
This commit is contained in:
Lennart Poettering 2018-11-08 12:36:06 +01:00 committed by GitHub
commit 010c9a247d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View file

@ -73,7 +73,8 @@
backslash is replaced by a space character. This may be used to wrap long lines. The limit on 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 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 use multiple directives, variable substitution, or other mechanism as appropriate for the given
file type.</para> 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.</para>
<example><programlisting>[Section A] <example><programlisting>[Section A]
KeyOne=value 1 KeyOne=value 1
@ -85,6 +86,12 @@ KeyTwo=value 2
Setting="something" "some thing" "…" Setting="something" "some thing" "…"
KeyTwo=value 2 \ KeyTwo=value 2 \
value 2 continued value 2 continued
[Section C]
KeyThree=value 2\
# this line is ignored
; this line is ignored too
value 2 continued
</programlisting></example> </programlisting></example>
<para>Boolean arguments used in configuration files can be written in <para>Boolean arguments used in configuration files can be written in

View file

@ -174,7 +174,7 @@ static int parse_line(
if (!*l) if (!*l)
return 0; return 0;
if (strchr(COMMENTS "\n", *l)) if (*l == '\n')
return 0; return 0;
include = first_word(l, ".include"); include = first_word(l, ".include");
@ -327,6 +327,9 @@ int config_parse(const char *unit,
return r; return r;
} }
if (strchr(COMMENTS, *buf))
continue;
l = buf; l = buf;
if (!(flags & CONFIG_PARSE_REFUSE_BOM)) { if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
char *q; char *q;

View file

@ -274,6 +274,18 @@ static const char* const config_file[] = {
"2\\\n" "2\\\n"
"3\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" "[Section]\n"
"setting1=1\\\n" /* continuation with extra trailing backslash at the end */ "setting1=1\\\n" /* continuation with extra trailing backslash at the end */
"2\\\n" "2\\\n"
@ -350,27 +362,27 @@ static void test_config_parse(unsigned i, const char *s) {
assert_se(streq(setting1, "1")); assert_se(streq(setting1, "1"));
break; break;
case 4 ... 5: case 4 ... 7:
assert_se(r == 0); assert_se(r == 0);
assert_se(streq(setting1, "1 2 3")); assert_se(streq(setting1, "1 2 3"));
break; break;
case 6: case 8:
assert_se(r == 0); assert_se(r == 0);
assert_se(streq(setting1, "1\\\\ \\\\2")); assert_se(streq(setting1, "1\\\\ \\\\2"));
break; break;
case 7: case 9:
assert_se(r == 0); assert_se(r == 0);
assert_se(streq(setting1, x1000("ABCD"))); assert_se(streq(setting1, x1000("ABCD")));
break; break;
case 8 ... 9: case 10 ... 11:
assert_se(r == 0); assert_se(r == 0);
assert_se(streq(setting1, x1000("ABCD") " foobar")); assert_se(streq(setting1, x1000("ABCD") " foobar"));
break; break;
case 10 ... 11: case 12 ... 13:
assert_se(r == -ENOBUFS); assert_se(r == -ENOBUFS);
assert_se(setting1 == NULL); assert_se(setting1 == NULL);
break; break;