changeset 22987:1611d0ce5001

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)
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 08 Jun 2018 23:17:04 +0300
parents 7009c54d937f
children c3c55b15fb6f
files src/lib/istream-try.c src/lib/test-istream-try.c
diffstat 2 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/istream-try.c	Tue Jul 24 15:00:19 2018 +0300
+++ b/src/lib/istream-try.c	Fri Jun 08 23:17:04 2018 +0300
@@ -76,6 +76,11 @@
 		}
 		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;
--- a/src/lib/test-istream-try.c	Tue Jul 24 15:00:19 2018 +0300
+++ b/src/lib/test-istream-try.c	Fri Jun 08 23:17:04 2018 +0300
@@ -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 @@
 	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();
+}