From d6bd50666947af2d9da7f6db186104d337cb43a5 Mon Sep 17 00:00:00 2001 From: Nikos Tsipinakis Date: Sat, 27 Apr 2019 10:30:02 +0300 Subject: [PATCH] Fix parsing of comments after a quoted string in the config Quoted values can still have a trailing comment that we need to remove before calling `add_value`. Otherwise, when trying to strip the quotes from the value `string_strip_quotes` only looks at the end which is inside the comment and so it won't find a matching end quote to strip. Fixes #626 --- src/option_parser.c | 14 +++++++++----- test/data/test-ini | 3 +++ test/option_parser.c | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/option_parser.c b/src/option_parser.c index 9dbca54..6b41e0d 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -355,17 +355,21 @@ int load_ini_file(FILE *fp) char *value = g_strstrip(equal + 1); char *quote = strchr(value, '"'); + char *value_end = NULL; if (quote) { - char *closing_quote = strchr(quote + 1, '"'); - if (!closing_quote) { + value_end = strchr(quote + 1, '"'); + if (!value_end) { LOG_W("Invalid config file at line %d: Missing '\"'.", line_num); continue; } } else { - char *comment = strpbrk(value, "#;"); - if (comment) - *comment = '\0'; + value_end = value; } + + char *comment = strpbrk(value_end, "#;"); + if (comment) + *comment = '\0'; + value = g_strstrip(value); if (!current_section) { diff --git a/test/data/test-ini b/test/data/test-ini index 0dab3e3..6201cd7 100644 --- a/test/data/test-ini +++ b/test/data/test-ini @@ -23,6 +23,9 @@ quoted = "A quoted string" quoted_with_quotes = "A string "with quotes"" unquoted_with_quotes = A" string with quotes" + quoted_comment = "String with a" # comment + unquoted_comment = String with a # comment + color_comment = "#ffffff" # comment [path] expand_tilde = ~/.path/to/tilde diff --git a/test/option_parser.c b/test/option_parser.c index 6de02a8..f399c26 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -54,6 +54,14 @@ TEST test_ini_get_string(void) ASSERT_STR_EQ("A\" string with quotes\"", (ptr = ini_get_string(string_section, "unquoted_with_quotes", ""))); free(ptr); + ASSERT_STR_EQ("String with a", (ptr = ini_get_string(string_section, "quoted_comment", ""))); + free(ptr); + ASSERT_STR_EQ("String with a", (ptr = ini_get_string(string_section, "unquoted_comment", ""))); + free(ptr); + ASSERT_STR_EQ("#ffffff", (ptr = ini_get_string(string_section, "color_comment", ""))); + free(ptr); + + ASSERT_STR_EQ("default value", (ptr = ini_get_string(string_section, "nonexistent", "default value"))); free(ptr);