From: Timo Sirainen Date: Fri, 28 Aug 2015 13:07:35 +0000 (+0200) Subject: lib: If var_expand_with_funcs() function returns NULL, it should be treated the same... X-Git-Tag: 2.2.19.rc1~164 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=654b46078129456bda90c5eb18014fb2858c302e;p=thirdparty%2Fdovecot%2Fcore.git lib: If var_expand_with_funcs() function returns NULL, it should be treated the same as "" The previous behavior was to return "%{foo:bar}" as "foo:bar}". --- diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index 8d479faa7e..a3542a4f8d 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -100,9 +100,58 @@ static void test_var_get_key_range(void) test_end(); } +static const char *test_var_expand_func1(const char *data, void *context) +{ + test_assert(*(int *)context == 0xabcdef); + return t_strdup_printf("<%s>", data); +} + +static const char *test_var_expand_func2(const char *data ATTR_UNUSED, + void *context ATTR_UNUSED) +{ + return ""; +} + +static const char *test_var_expand_func3(const char *data ATTR_UNUSED, + void *context ATTR_UNUSED) +{ + return NULL; +} + +static void test_var_expand_with_funcs(void) +{ + static struct var_expand_test tests[] = { + { "%{func1}", "<>" }, + { "%{func1:foo}", "" }, + { "%{func2}", "" }, + { "%{func3}", "" } + }; + static struct var_expand_table table[] = { + { '\0', NULL, NULL } + }; + static const struct var_expand_func_table func_table[] = { + { "func1", test_var_expand_func1 }, + { "func2", test_var_expand_func2 }, + { "func3", test_var_expand_func3 }, + { NULL, NULL } + }; + string_t *str = t_str_new(128); + unsigned int i; + int ctx = 0xabcdef; + + test_begin("var_expand_with_funcs"); + for (i = 0; i < N_ELEMENTS(tests); i++) { + str_truncate(str, 0); + var_expand_with_funcs(str, tests[i].in, table, func_table, &ctx); + test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i); + } + test_end(); +} + void test_var_expand(void) { test_var_expand_ranges(); test_var_expand_builtin(); test_var_get_key_range(); + test_var_expand_with_funcs(); } diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 9ff7680aed..8dcbec72e0 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -170,14 +170,20 @@ static const char * var_expand_func(const struct var_expand_func_table *func_table, const char *key, const char *data, void *context) { - if (strcmp(key, "env") == 0) - return getenv(data); + const char *value; + + if (strcmp(key, "env") == 0) { + value = getenv(data); + return value != NULL ? value : ""; + } if (func_table == NULL) return NULL; for (; func_table->key != NULL; func_table++) { - if (strcmp(func_table->key, key) == 0) - return func_table->func(data, context); + if (strcmp(func_table->key, key) == 0) { + value = func_table->func(data, context); + return value != NULL ? value : ""; + } } return NULL; }