]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: var_expand*() - If string ends with '%', return error
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Sat, 2 Dec 2023 23:30:18 +0000 (01:30 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:10 +0000 (12:34 +0200)
src/lib/test-var-expand.c
src/lib/var-expand.c

index c519bd401e82f374adc47bcffe7c4ac59f2ede63..76f259d32eb43479d8a55a9a0a4dc0621d57c229 100644 (file)
@@ -65,6 +65,8 @@ static void test_var_expand_builtin(void)
                { "%1.2M{nonexistent:default}", "UNSUPPORTED_VARIABLE_nonexistent", 0 },
                { "%x", "UNSUPPORTED_VARIABLE_x", 0 },
                { "%5Mm", "UNSUPPORTED_VARIABLE_m", 0 },
+
+               { "%", "", -1 },
        };
        static const struct var_expand_table table[] = {
                { 'v', "value", NULL },
@@ -85,7 +87,7 @@ static void test_var_expand_builtin(void)
        for (i = 0; i < N_ELEMENTS(tests); i++) {
                str_truncate(str, 0);
                test_assert_idx(var_expand(str, tests[i].in, table, &error) == tests[i].ret, i);
-               test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
+               test_assert_strcmp_idx(tests[i].out, str_c(str), i);
        }
        test_end();
 }
index 1d7fe7d04c823d653374493488a131743fbca54e..8ef40a7723c509441b8664333cf94fa2edabc038 100644 (file)
@@ -626,10 +626,7 @@ int var_expand_with_arrays(string_t *dest, const char *str,
                                modifier_count++;
                        }
 
-                       if (*str == '\0')
-                               break;
-
-                       var = NULL;
+                       var = "";
                        if (*str == '{' && strchr(str, '}') != NULL) {
                                /* %{long_key} */
                                unsigned int ctr = 1;
@@ -662,6 +659,10 @@ int var_expand_with_arrays(string_t *dest, const char *str,
                        }
                        i_assert(var != NULL);
 
+                       if (*str == '\0') {
+                               *error_r = "%variable ends unexpectedly";
+                               return -1;
+                       }
                        if (final_ret > ret)
                                final_ret = ret;