dovecot-2.2: lib: If var_expand_with_funcs() function returns NU...

dovecot at dovecot.org dovecot at dovecot.org
Fri Aug 28 13:08:38 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/061c21b5c5e4
changeset: 19030:061c21b5c5e4
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Aug 28 15:07:35 2015 +0200
description:
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}".

diffstat:

 src/lib/test-var-expand.c |  49 +++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/var-expand.c      |  14 +++++++++---
 2 files changed, 59 insertions(+), 4 deletions(-)

diffs (90 lines):

diff -r 170f2b9f4977 -r 061c21b5c5e4 src/lib/test-var-expand.c
--- a/src/lib/test-var-expand.c	Fri Aug 28 14:43:35 2015 +0200
+++ b/src/lib/test-var-expand.c	Fri Aug 28 15:07:35 2015 +0200
@@ -100,9 +100,58 @@
 	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();
 }
diff -r 170f2b9f4977 -r 061c21b5c5e4 src/lib/var-expand.c
--- a/src/lib/var-expand.c	Fri Aug 28 14:43:35 2015 +0200
+++ b/src/lib/var-expand.c	Fri Aug 28 15:07:35 2015 +0200
@@ -170,14 +170,20 @@
 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;
 }


More information about the dovecot-cvs mailing list