Skip to content

Commit

Permalink
lib-compression: ostream-zlib - Require o_stream_finish() to write th…
Browse files Browse the repository at this point in the history
…e trailer

It's now possible to call o_stream_flush() without the trailer getting
written.

It's also now required to call o_stream_finish() to write the trailer before
closing the ostream. This guarantees that the caller checks that the
trailer was successfully written. This isn't needed if the ostream already
failed before writing the trailer.
  • Loading branch information
sirainen authored and Timo Sirainen committed Oct 30, 2017
1 parent 4805c8a commit 6fb8641
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib-compression/ostream-zlib.c
Expand Up @@ -32,7 +32,8 @@ static void o_stream_zlib_close(struct iostream_private *stream,
{
struct zlib_ostream *zstream = (struct zlib_ostream *)stream;

(void)o_stream_flush(&zstream->ostream.ostream);
i_assert(zstream->ostream.finished ||
zstream->ostream.ostream.stream_errno != 0);
(void)deflateEnd(&zstream->zs);
if (close_parent)
o_stream_close(zstream->ostream.parent);
Expand Down Expand Up @@ -230,7 +231,7 @@ static int o_stream_zlib_flush(struct ostream_private *stream)
struct zlib_ostream *zstream = (struct zlib_ostream *)stream;
int ret;

if (o_stream_zlib_send_flush(zstream, TRUE) < 0)
if (o_stream_zlib_send_flush(zstream, stream->finished) < 0)
return -1;

ret = o_stream_flush(stream->parent);
Expand Down
1 change: 1 addition & 0 deletions src/lib-compression/test-compression.c
Expand Up @@ -62,6 +62,7 @@ static void test_compression_handler(const struct compression_handler *handler)
test_assert(o_stream_send(output, buf, sizeof(buf)) == sizeof(buf));
}

test_assert(o_stream_finish(output) > 0);
o_stream_destroy(&output);
o_stream_destroy(&file_output);
sha1_result(&sha1, output_sha1);
Expand Down

0 comments on commit 6fb8641

Please sign in to comment.