diff --git a/util/compression.h b/util/compression.h index a318c0cab..274f1487e 100644 --- a/util/compression.h +++ b/util/compression.h @@ -745,9 +745,6 @@ inline bool Zlib_Compress(const CompressionInfo& info, output_header_len = compression::PutDecompressedSizeInfo( output, static_cast(length)); } - // Resize output to be the plain data length. - // This may not be big enough if the compression actually expands data. - output->resize(output_header_len + length); // The memLevel parameter specifies how much memory should be allocated for // the internal compression state. @@ -781,12 +778,16 @@ inline bool Zlib_Compress(const CompressionInfo& info, } } + // Get an upper bound on the compressed size. + size_t upper_bound = deflateBound(&_stream, length); + output->resize(output_header_len + upper_bound); + // Compress the input, and put compressed data in output. _stream.next_in = (Bytef*)input; _stream.avail_in = static_cast(length); // Initialize the output size. - _stream.avail_out = static_cast(length); + _stream.avail_out = static_cast(upper_bound); _stream.next_out = reinterpret_cast(&(*output)[output_header_len]); bool compressed = false;