]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: var_has_key() properly ignores key=='\0' now.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 2 Aug 2016 10:41:07 +0000 (13:41 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 2 Aug 2016 10:42:43 +0000 (13:42 +0300)
src/lib/test-var-expand.c
src/lib/var-expand.c
src/lib/var-expand.h

index 33ed0f3ed3ee87960e098b0aaa2003dbc5f72329..080a0739a4320ba381cf7374f54d167517bdb733 100644 (file)
@@ -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();
 }
index 8135b3a19761fddbc5f0a3cf2df0cecfdee34025..b4093ada427c0cd3b435b78c500e391a64095e25 100644 (file)
@@ -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) {
index ac554ff15cde6ec10bb90c45ae6dc20b7379a1d1..ccaaeac9f33a9745c426f8fa3093cd095a30cb2f 100644 (file)
@@ -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 *