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
This commit is contained in:
Nikos Tsipinakis 2019-04-27 10:30:02 +03:00
parent 6cab789154
commit d6bd506669
3 changed files with 20 additions and 5 deletions

View File

@ -355,17 +355,21 @@ int load_ini_file(FILE *fp)
char *value = g_strstrip(equal + 1); char *value = g_strstrip(equal + 1);
char *quote = strchr(value, '"'); char *quote = strchr(value, '"');
char *value_end = NULL;
if (quote) { if (quote) {
char *closing_quote = strchr(quote + 1, '"'); value_end = strchr(quote + 1, '"');
if (!closing_quote) { if (!value_end) {
LOG_W("Invalid config file at line %d: Missing '\"'.", line_num); LOG_W("Invalid config file at line %d: Missing '\"'.", line_num);
continue; continue;
} }
} else { } else {
char *comment = strpbrk(value, "#;"); value_end = value;
if (comment)
*comment = '\0';
} }
char *comment = strpbrk(value_end, "#;");
if (comment)
*comment = '\0';
value = g_strstrip(value); value = g_strstrip(value);
if (!current_section) { if (!current_section) {

View File

@ -23,6 +23,9 @@
quoted = "A quoted string" quoted = "A quoted string"
quoted_with_quotes = "A string "with quotes"" quoted_with_quotes = "A string "with quotes""
unquoted_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] [path]
expand_tilde = ~/.path/to/tilde expand_tilde = ~/.path/to/tilde

View File

@ -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", ""))); ASSERT_STR_EQ("A\" string with quotes\"", (ptr = ini_get_string(string_section, "unquoted_with_quotes", "")));
free(ptr); 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"))); ASSERT_STR_EQ("default value", (ptr = ini_get_string(string_section, "nonexistent", "default value")));
free(ptr); free(ptr);