From ddeb3f5d4b7ce67c23fde0ad149fb06b29a92f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Nov 2019 11:16:17 +0100 Subject: [PATCH] shared/conf-parser: allow sections to be silently ignored with new -Section syntax If we ignore any uknown section, we will not be able to show any warning if a typo in a section name is made. Let's reverse our approach, and explicitly list sections to ignore instead. I opted to make use the same section list for this, instead of adding a second list, because this list is passed through to many functions and adding yet another parameter to the long signature would be very noisy. --- src/shared/conf-parser.c | 13 ++++++++++++- src/test/test-conf-parser.c | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 648ac1aa94..90b31148f3 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -221,8 +221,19 @@ static int parse_line( return -ENOMEM; if (sections && !nulstr_contains(sections, n)) { + bool ignore = flags & CONFIG_PARSE_RELAXED; + const char *t; - if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-")) + ignore = ignore || startswith(n, "X-"); + + if (!ignore) + NULSTR_FOREACH(t, sections) + if (streq_ptr(n, startswith(t, "-"))) { + ignore = true; + break; + } + + if (!ignore) log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n); free(n); diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index 597265efa6..b392a2b5ec 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -299,6 +299,15 @@ static const char* const config_file[] = { "[Section]\n" "setting1=" /* many continuation lines, together above the limit */ x1000(x1000("x") x10("abcde") "\\\n") "xxx", + + "[Section]\n" + "setting1=2\n" + "[NoWarnSection]\n" + "setting1=3\n" + "[WarnSection]\n" + "setting1=3\n" + "[X-Section]\n" + "setting1=3\n", }; static void test_config_parse(unsigned i, const char *s) { @@ -325,14 +334,12 @@ static void test_config_parse(unsigned i, const char *s) { const char *sections, ConfigItemLookup lookup, const void *table, - bool relaxed, - bool allow_include, - bool warn, + ConfigParseFlags flags, void *userdata) */ r = config_parse(NULL, name, f, - "Section\0", + "Section\0-NoWarnSection\0", config_item_table_lookup, items, CONFIG_PARSE_WARN, NULL); @@ -366,6 +373,11 @@ static void test_config_parse(unsigned i, const char *s) { assert_se(r == -ENOBUFS); assert_se(setting1 == NULL); break; + + case 17: + assert_se(r == 0); + assert_se(streq(setting1, "2")); + break; } }