From: Timo Sirainen Date: Tue, 2 Aug 2016 10:41:07 +0000 (+0300) Subject: lib: var_has_key() properly ignores key=='\0' now. X-Git-Tag: 2.3.0.rc1~3273 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e539ff946adcbc68793adb6d9d84621dce3bf46b;p=thirdparty%2Fdovecot%2Fcore.git lib: var_has_key() properly ignores key=='\0' now. --- diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index 33ed0f3ed3..080a0739a4 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -171,6 +171,31 @@ static void test_var_get_key(void) 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 @@ void test_var_expand(void) test_var_get_key_range(); test_var_expand_with_funcs(); test_var_get_key(); + test_var_has_key(); } diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 8135b3a197..b4093ada42 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -473,7 +473,7 @@ bool var_has_key(const char *str, char key, const char *long_key) 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) { diff --git a/src/lib/var-expand.h b/src/lib/var-expand.h index ac554ff15c..ccaaeac9f3 100644 --- a/src/lib/var-expand.h +++ b/src/lib/var-expand.h @@ -32,7 +32,8 @@ char var_get_key(const char *str) ATTR_PURE; 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 *