From: Timo Sirainen Date: Sat, 2 Dec 2023 23:30:18 +0000 (+0200) Subject: lib: var_expand*() - If string ends with '%', return error X-Git-Tag: 2.4.1~1277 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a6f337e2e4cdb8a4367f24118af21462af2d653f;p=thirdparty%2Fdovecot%2Fcore.git lib: var_expand*() - If string ends with '%', return error --- diff --git a/src/lib/test-var-expand.c b/src/lib/test-var-expand.c index c519bd401e..76f259d32e 100644 --- a/src/lib/test-var-expand.c +++ b/src/lib/test-var-expand.c @@ -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(); } diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index 1d7fe7d04c..8ef40a7723 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -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;