]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: If var_expand_with_funcs() function returns NULL, it should be treated the same...
authorTimo Sirainen <tss@iki.fi>
Fri, 28 Aug 2015 13:07:35 +0000 (15:07 +0200)
committerTimo Sirainen <tss@iki.fi>
Fri, 28 Aug 2015 13:07:35 +0000 (15:07 +0200)
The previous behavior was to return "%{foo:bar}" as "foo:bar}".

src/lib/test-var-expand.c
src/lib/var-expand.c

index 8d479faa7e2cf0ef35cd66a8e19877f6ee7064f2..a3542a4f8db64475b1ee1c4d49aeba8bac5d17e3 100644 (file)
@@ -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}", "<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();
 }
index 9ff7680aed6dc43f7083e6bc4ffefab60b8fe31f..8dcbec72e0361a036f14f1415448b13f492ee66b 100644 (file)
@@ -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;
 }