changeset 20551:64f547f5bd86

lib: var_has_key() properly ignores key=='\0' now.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 02 Aug 2016 13:41:07 +0300
parents ba37ec73c6aa
children cc06003a338b
files src/lib/test-var-expand.c src/lib/var-expand.c src/lib/var-expand.h
diffstat 3 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/test-var-expand.c	Tue Aug 02 13:38:25 2016 +0300
+++ b/src/lib/test-var-expand.c	Tue Aug 02 13:41:07 2016 +0300
@@ -171,6 +171,31 @@
 	test_end();
 }
 
+static void test_var_has_key(void)
+{
+	static struct {
+		const char *str;
+		char key;
+		const char *long_key;
+		bool result;
+	} tests[] = {
+		{ "%x%y", 'x', NULL, TRUE },
+		{ "%x%y", 'y', NULL, TRUE },
+		{ "%x%y", 'z', NULL, FALSE },
+		{ "%{foo}", 'f', NULL, FALSE },
+		{ "%{foo}", 'o', NULL, FALSE },
+		{ "%{foo}", '\0', "foo", TRUE },
+		{ "%{foo}", 'o', "foo", TRUE },
+		{ "%2.5Mx%y", 'x', NULL, TRUE },
+		{ "%2.5M{foo}", '\0', "foo", TRUE },
+	};
+
+	test_begin("var_has_key");
+	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
+		test_assert_idx(var_has_key(tests[i].str, tests[i].key, tests[i].long_key) == tests[i].result, i);
+	test_end();
+}
+
 void test_var_expand(void)
 {
 	test_var_expand_ranges();
@@ -178,4 +203,5 @@
 	test_var_get_key_range();
 	test_var_expand_with_funcs();
 	test_var_get_key();
+	test_var_has_key();
 }
--- a/src/lib/var-expand.c	Tue Aug 02 13:38:25 2016 +0300
+++ b/src/lib/var-expand.c	Tue Aug 02 13:41:07 2016 +0300
@@ -473,7 +473,7 @@
 		if (*str == '%' && str[1] != '\0') {
 			str++;
 			c = var_get_key(str);
-			if (c == key)
+			if (c == key && key != '\0')
 				return TRUE;
 
 			if (c == '{' && long_key != NULL) {
--- a/src/lib/var-expand.h	Tue Aug 02 13:38:25 2016 +0300
+++ b/src/lib/var-expand.h	Tue Aug 02 13:41:07 2016 +0300
@@ -32,7 +32,8 @@
    keys size=1, while for e.g. %{key} size=3 and idx points to 'k'. */
 void var_get_key_range(const char *str, unsigned int *idx_r,
 		       unsigned int *size_r);
-/* Returns TRUE if key variable is used in the string. long_key may be NULL. */
+/* Returns TRUE if key variable is used in the string.
+   If key is '\0', it's ignored. If long_key is NULL, it's ignored. */
 bool var_has_key(const char *str, char key, const char *long_key) ATTR_PURE;
 
 const struct var_expand_table *