Fix successful decompress reported as Error::Read (#1612)

* Fix successful decompress reported as Error::Read

Streams less than 4096 bytes are sometimes reported as failed reads because stream_.avail_in is not reduced to 0. The next iteration of the loop finds `prev_avail_in == strm_.avail_in` and return false. `ret = inflate(...)` returns Z_STREAM_END on the first iteration of the loop indicating that inflate is finished. This fix prevents the second iteration of the loop from failing.

* Fix successful decompress reported as Error::Read

- Add unit tests for raw deflate that illustrates the decompression failure when there are extra trailing bytes
This commit is contained in:
bdenhollander 2023-07-11 18:35:27 -04:00 committed by GitHub
parent 52d8dd41f1
commit ee625232a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 5 deletions

View file

@ -3384,16 +3384,12 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
data += strm_.avail_in;
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
while (strm_.avail_in > 0) {
while (strm_.avail_in > 0 && ret == Z_OK) {
strm_.avail_out = static_cast<uInt>(buff.size());
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
auto prev_avail_in = strm_.avail_in;
ret = inflate(&strm_, Z_NO_FLUSH);
if (prev_avail_in - strm_.avail_in == 0) { return false; }
assert(ret != Z_STREAM_ERROR);
switch (ret) {
case Z_NEED_DICT: