From: Sergey Kitov Date: Tue, 20 Jun 2017 09:11:37 +0000 (+0300) Subject: lib-imap: imap_append_nstring_nolf() - fix crash with datastack_pool strings X-Git-Tag: 2.2.31.rc1~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c65646e7a1310caa312af4d474c4989dcfca944b;p=thirdparty%2Fdovecot%2Fcore.git lib-imap: imap_append_nstring_nolf() - fix crash with datastack_pool strings T_BEGIN .. T_END is not used, when string_t is allocated from datastack pool, unit test updated to verify the fix. --- diff --git a/src/lib-imap/imap-quote.c b/src/lib-imap/imap-quote.c index 3e751245d3..260176899c 100644 --- a/src/lib-imap/imap-quote.c +++ b/src/lib-imap/imap-quote.c @@ -90,31 +90,36 @@ void imap_append_nstring(string_t *dest, const char *src) imap_append_quoted(dest, src); } -void imap_append_nstring_nolf(string_t *dest, const char *src) +static void remove_newlines_and_append(string_t *dest, const char *src) { - string_t *src_nolf; size_t src_len; + string_t *src_nolf; + src_len = strlen(src); + src_nolf = t_str_new(src_len + 1); + for (size_t i = 0; i < src_len; ++i) { + if (src[i] != '\r' && src[i] != '\n') { + str_append_c(src_nolf, src[i]); + } else if (src[i+1] != ' ' && + src[i+1] != '\t' && + src[i+1] != '\r' && + src[i+1] != '\n' && + src[i+1] != '\0') { + /* ensure whitespace between lines if new line doesn't start with whitespace */ + str_append_c(src_nolf, ' '); + } + } + imap_append_nstring(dest, str_c(src_nolf)); +} + +void imap_append_nstring_nolf(string_t *dest, const char *src) +{ if (src == NULL || strpbrk(src, "\r\n") == NULL) imap_append_nstring(dest, src); - else { - T_BEGIN { - src_len = strlen(src); - src_nolf = t_str_new(src_len + 1); - for (size_t i = 0; i < src_len; ++i) { - if (src[i] != '\r' && src[i] != '\n') { - str_append_c(src_nolf, src[i]); - } else if (src[i+1] != ' ' && - src[i+1] != '\t' && - src[i+1] != '\r' && - src[i+1] != '\n' && - src[i+1] != '\0') { - /* ensure whitespace between lines if new line doesn't start with whitespace */ - str_append_c(src_nolf, ' '); - } - } - imap_append_nstring(dest, str_c(src_nolf)); - } T_END; - } + else if (buffer_get_pool(dest)->datastack_pool) + remove_newlines_and_append(dest, src); + else T_BEGIN { + remove_newlines_and_append(dest, src); + } T_END; } void imap_append_quoted(string_t *dest, const char *src) diff --git a/src/lib-imap/test-imap-quote.c b/src/lib-imap/test-imap-quote.c index 9b94dd638d..03d61eacc7 100644 --- a/src/lib-imap/test-imap-quote.c +++ b/src/lib-imap/test-imap-quote.c @@ -137,16 +137,24 @@ static void test_imap_append_nstring_nolf(void) { "foo\r bar", "\"foo bar\"" }, { "\nfoo\r bar\r\n", "\" foo bar\"" } }; - string_t *str = t_str_new(128); unsigned int i; test_begin("test_imap_append_nstring_nolf()"); - for (i = 0; i < N_ELEMENTS(tests); i++) { + for (i = 0; i < N_ELEMENTS(tests); i++) T_BEGIN { + string_t *str = t_str_new(1); + string_t *str2 = str_new(default_pool, 1); + str_truncate(str, 0); imap_append_nstring_nolf(str, tests[i].input); test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i); - } + + str_truncate(str2, 0); + imap_append_nstring_nolf(str2, tests[i].input); + test_assert_idx(strcmp(tests[i].output, str_c(str2)) == 0, i); + + str_free(&str2); + } T_END; test_end(); }