diff --git a/src/lib-mail/message-snippet.c b/src/lib-mail/message-snippet.c index b42800a252..012d82ced5 100644 --- a/src/lib-mail/message-snippet.c +++ b/src/lib-mail/message-snippet.c @@ -60,6 +60,10 @@ static bool snippet_generate(struct snippet_context *ctx, count += 2; /* because we skip +1 next */ break; } + if (data[i] == '\0') { + /* skip NULs without increasing snippet size */ + break; + } if (data[i] == '\r' || data[i] == '\n' || data[i] == '\t' || data[i] == ' ') { /* skip any leading whitespace */ diff --git a/src/lib-mail/test-message-snippet.c b/src/lib-mail/test-message-snippet.c index 557d429b9f..1401b8fc41 100644 --- a/src/lib-mail/test-message-snippet.c +++ b/src/lib-mail/test-message-snippet.c @@ -84,10 +84,26 @@ static void test_message_snippet(void) test_end(); } +static void test_message_snippet_nuls(void) +{ + const char input_text[] = "\nfoo\0bar"; + string_t *str = t_str_new(128); + struct istream *input; + + test_begin("message snippet with NULs"); + + input = i_stream_create_from_data(input_text, sizeof(input_text)-1); + test_assert(message_snippet_generate(input, 5, str) == 0); + test_assert(strcmp(str_c(str), "fooba") == 0); + i_stream_destroy(&input); + test_end(); +} + int main(void) { static void (*const test_functions[])(void) = { test_message_snippet, + test_message_snippet_nuls, NULL }; return test_run(test_functions);