]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: istream-seekable - Fix returning stream size
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 22 Oct 2020 19:46:35 +0000 (22:46 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 23 Oct 2020 09:19:23 +0000 (09:19 +0000)
The returned size may have been truncated.

src/lib/istream-seekable.c
src/lib/test-istream-seekable.c

index 9750cbd67eb7b58c34ca85a6d86c30bb34be4ed4..fdaaaaee6a259909cb9240630d139f82edf6362d 100644 (file)
@@ -162,7 +162,8 @@ static ssize_t read_more(struct seekable_istream *sstream)
                sstream->cur_input = sstream->input[sstream->cur_idx++];
                if (sstream->cur_input == NULL) {
                        /* last one, EOF */
-                       sstream->size = sstream->istream.istream.v_offset;
+                       sstream->size = sstream->istream.istream.v_offset +
+                               (sstream->istream.pos - sstream->istream.skip);
                        sstream->istream.istream.eof = TRUE;
                        /* Now that EOF is reached, the stream can't return 0
                           anymore. Callers can now use this stream in places
index e57eebadfbfdeed8a72e3c4ba900e9ea61f170fb..7d05347597df56d6f5e4d0ef33991a385902063a 100644 (file)
@@ -216,6 +216,23 @@ static void test_istream_seekable_invalid_read(void)
        test_end();
 }
 
+static void test_istream_seekable_get_size(void)
+{
+       test_begin("istream seekable get size");
+       struct istream *str_input = test_istream_create("123456");
+       str_input->seekable = FALSE;
+       struct istream *seek_inputs[] = { str_input, NULL };
+       struct istream *input = i_stream_create_seekable(seek_inputs, 32, fd_callback, NULL);
+       uoff_t size;
+       test_assert(i_stream_read(input) == 6);
+       test_assert(i_stream_read(input) == -1);
+       test_assert(i_stream_get_size(input, TRUE, &size) == 1 &&
+                   size == 6);
+       i_stream_unref(&input);
+       i_stream_unref(&str_input);
+       test_end();
+}
+
 void test_istream_seekable(void)
 {
        unsigned int i;
@@ -234,4 +251,5 @@ void test_istream_seekable(void)
        test_istream_seekable_eof();
        test_istream_seekable_early_end();
        test_istream_seekable_invalid_read();
+       test_istream_seekable_get_size();
 }