From 619fb92a081c4182a836f58153801daa396d5c6b Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 8 Jun 2018 23:17:04 +0300 Subject: [PATCH] lib: istream-try - Don't assert-crash with empty parent istream Fixes: Panic: file istream.c: line 327 (i_stream_read_memarea): assertion failed: (stream->eof) --- src/lib/istream-try.c | 5 +++++ src/lib/test-istream-try.c | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/istream-try.c b/src/lib/istream-try.c index b033b776e3..df94a48669 100644 --- a/src/lib/istream-try.c +++ b/src/lib/istream-try.c @@ -76,6 +76,11 @@ static int i_stream_try_detect(struct try_istream *tstream) } if (ret == 0) return 0; + if (try_input->stream_errno == 0) { + /* empty file */ + tstream->istream.istream.eof = TRUE; + return -1; + } if (try_input->stream_errno != EINVAL) { tstream->istream.istream.stream_errno = try_input->stream_errno; diff --git a/src/lib/test-istream-try.c b/src/lib/test-istream-try.c index 39a4185e73..91ac37ddd1 100644 --- a/src/lib/test-istream-try.c +++ b/src/lib/test-istream-try.c @@ -4,7 +4,7 @@ #include "istream.h" #include "istream-try.h" -void test_istream_try(void) +static void test_istream_try_normal(void) { bool finished = FALSE; @@ -128,3 +128,27 @@ void test_istream_try(void) i_assert(finished); test_end(); } + +static void test_istream_try_empty(void) +{ + test_begin("istream try empty stream"); + struct istream *test_inputs[] = { + test_istream_create(""), + test_istream_create(""), + NULL + }; + struct istream *try_input = istream_try_create(test_inputs); + test_assert(i_stream_read(try_input) == -1); + test_assert(try_input->eof); + test_assert(try_input->stream_errno == 0); + i_stream_unref(&test_inputs[0]); + i_stream_unref(&test_inputs[1]); + i_stream_unref(&try_input); + test_end(); +} + +void test_istream_try(void) +{ + test_istream_try_normal(); + test_istream_try_empty(); +} -- 2.47.3