From 15490816da0f8b651d67acef9c7f4e5bf9652ce2 Mon Sep 17 00:00:00 2001 From: Daniel Pinto Date: Sun, 22 Sep 2019 22:30:48 +0100 Subject: [PATCH 1/2] Fix bug when %including folder with comment only files. #31408 When processing a %included folder, a bug caused the pointer to the last element of the options list to be set to NULL when processing a file with only comments or whitepace. This could cause options from other files on the same folder to be discarded depending on the lines after the affected %include. --- changes/bug31408 | 4 +++ src/lib/fs/conffile.c | 10 ++++--- src/test/test_config.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 changes/bug31408 diff --git a/changes/bug31408 b/changes/bug31408 new file mode 100644 index 0000000000..7a6744cee4 --- /dev/null +++ b/changes/bug31408 @@ -0,0 +1,4 @@ + o Major bugfixes (torrc): + - Fix configuration files in a %included folder containing a + configuration file with only comments or whitespace being + ignored. Fixes bug 31408; bugfix on 0.4.0.5. diff --git a/src/lib/fs/conffile.c b/src/lib/fs/conffile.c index 7bb2f23931..0d5d56b335 100644 --- a/src/lib/fs/conffile.c +++ b/src/lib/fs/conffile.c @@ -153,16 +153,18 @@ config_process_include(const char *path, int recursion_level, int extended, int rv = -1; SMARTLIST_FOREACH_BEGIN(config_files, const char *, config_file) { config_line_t *included_config = NULL; + config_line_t *included_config_last = NULL; if (config_get_included_config(config_file, recursion_level, extended, - &included_config, list_last, + &included_config, &included_config_last, opened_lst) < 0) { goto done; } *next = included_config; - if (*list_last) - next = &(*list_last)->next; - + if (included_config_last) { + next = &included_config_last->next; + *list_last = included_config_last; + } } SMARTLIST_FOREACH_END(config_file); *list = ret_list; rv = 0; diff --git a/src/test/test_config.c b/src/test/test_config.c index 0de6b12919..8f011ce1f1 100644 --- a/src/test/test_config.c +++ b/src/test/test_config.c @@ -5286,6 +5286,73 @@ test_config_include_folder_order(void *data) tor_free(dir); } +static void +test_config_include_blank_file_last(void *data) +{ + (void)data; + + config_line_t *result = NULL; + char *torrcd = NULL; + char *path = NULL; + char *dir = tor_strdup(get_fname("test_include_blank_file_last")); + tt_ptr_op(dir, OP_NE, NULL); + +#ifdef _WIN32 + tt_int_op(mkdir(dir), OP_EQ, 0); +#else + tt_int_op(mkdir(dir, 0700), OP_EQ, 0); +#endif + + tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d"); + +#ifdef _WIN32 + tt_int_op(mkdir(torrcd), OP_EQ, 0); +#else + tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0); +#endif + + tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_1st"); + tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0); + tor_free(path); + + tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "bb_2nd"); + tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0); + tor_free(path); + + tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "cc_comment"); + tt_int_op(write_str_to_file(path, "# comment only\n", 0), OP_EQ, 0); + tor_free(path); + + char torrc_contents[1000]; + tor_snprintf(torrc_contents, sizeof(torrc_contents), + "%%include %s\n" + "Test 3\n", + torrcd); + + int include_used; + tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used, + NULL), OP_EQ, 0); + tt_ptr_op(result, OP_NE, NULL); + tt_int_op(include_used, OP_EQ, 1); + + int len = 0; + config_line_t *next; + for (next = result; next != NULL; next = next->next) { + char expected[10]; + tor_snprintf(expected, sizeof(expected), "%d", len + 1); + tt_str_op(next->key, OP_EQ, "Test"); + tt_str_op(next->value, OP_EQ, expected); + len++; + } + tt_int_op(len, OP_EQ, 3); + + done: + config_free_lines(result); + tor_free(torrcd); + tor_free(path); + tor_free(dir); +} + static void test_config_include_path_syntax(void *data) { @@ -5848,6 +5915,7 @@ struct testcase_t config_tests[] = { CONFIG_TEST(include_recursion_before_after, 0), CONFIG_TEST(include_recursion_after_only, 0), CONFIG_TEST(include_folder_order, 0), + CONFIG_TEST(include_blank_file_last, 0), CONFIG_TEST(include_path_syntax, 0), CONFIG_TEST(include_not_processed, 0), CONFIG_TEST(include_has_include, 0), From 0614f839054a52e6e1a79a366fcc70da0691df66 Mon Sep 17 00:00:00 2001 From: teor Date: Mon, 23 Sep 2019 11:11:50 +1000 Subject: [PATCH 2/2] changes: use correct bugfix release, and reword changes file for 31408 --- changes/bug31408 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/changes/bug31408 b/changes/bug31408 index 7a6744cee4..3e4ffa927d 100644 --- a/changes/bug31408 +++ b/changes/bug31408 @@ -1,4 +1,5 @@ o Major bugfixes (torrc): - - Fix configuration files in a %included folder containing a - configuration file with only comments or whitespace being - ignored. Fixes bug 31408; bugfix on 0.4.0.5. + - Stop ignoring torrc options after an %include directive, when the + included directory ends with a file that does not contain any config + options. (But does contain comments or whitespace.) + Fixes bug 31408; bugfix on 0.3.1.1-alpha.