From 7ca5bd71af269d64f961e389985d7f51d84ac642 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 12 Mar 2020 12:58:11 +0200 Subject: [PATCH] lib: ostream-file - Fix using sendfile() when input stream is ends unexpectedly early Handle this the same as if writing was done without sendfile(), i.e. ignore the problem and just return EOF in input stream early. --- src/lib/ostream-file.c | 9 +++++++-- src/lib/test-ostream-file.c | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lib/ostream-file.c b/src/lib/ostream-file.c index 869a670ddc..b1f3f9f592 100644 --- a/src/lib/ostream-file.c +++ b/src/lib/ostream-file.c @@ -777,8 +777,13 @@ io_stream_sendfile(struct ostream_private *outstream, ret = safe_sendfile(foutstream->fd, in_fd, &offset, MAX_SSIZE_T(send_size)); if (ret <= 0) { - if (ret == 0) - break; + if (ret == 0) { + /* Unexpectedly early EOF at input */ + i_stream_seek(instream, v_offset); + instream->eof = TRUE; + *res_r = OSTREAM_SEND_ISTREAM_RESULT_FINISHED; + return TRUE; + } if (foutstream->file) { if (errno == EINTR) { /* automatically retry */ diff --git a/src/lib/test-ostream-file.c b/src/lib/test-ostream-file.c index d61c5a819c..d4a9eff88e 100644 --- a/src/lib/test-ostream-file.c +++ b/src/lib/test-ostream-file.c @@ -164,8 +164,16 @@ static void test_ostream_file_send_istream_sendfile(void) test_assert(read(sock_fd[1], buf, sizeof(buf)) == 4 && memcmp(buf, "defg", 4) == 0); i_stream_unref(&input2); - i_stream_unref(&input); + /* test reading past EOF */ + i_stream_seek(input, 0); + input2 = i_stream_create_limit(input, 20); + test_assert(o_stream_send_istream(output, input2) == OSTREAM_SEND_ISTREAM_RESULT_FINISHED); + test_assert(input2->v_offset == 10); + test_assert(output->offset == 14); + i_stream_unref(&input2); + + i_stream_unref(&input); o_stream_destroy(&output); i_close_fd(&sock_fd[1]); -- 2.47.3