From 9367e45e667198323c3b4372196b8fe8796aec47 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 7 Apr 2025 23:20:08 +0200 Subject: [PATCH] Fix broken buffer handling in ZlibDecompressor 40a5e16e21ba9a0663fc93919291c1bf0e5c2e3a added the broken reserve() call and only by chance did 0a56b18cfbb583649bdbc7f0a379df5d63ea2880 not break it further because I forgot to remove the unconditional resize(). I should read my own code changes more often. --- src/ZlibDecompressor.cpp | 7 ++++--- src/ZstdDecompressor.cpp | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ZlibDecompressor.cpp b/src/ZlibDecompressor.cpp index d6b6514..0f9a544 100644 --- a/src/ZlibDecompressor.cpp +++ b/src/ZlibDecompressor.cpp @@ -48,16 +48,17 @@ void ZlibDecompressor::decompress(ustring &buffer) strm.next_in = const_cast(data); strm.avail_in = size; - buffer.resize(BUFSIZE); + if (buffer.empty()) + buffer.resize(BUFSIZE); strm.next_out = &buffer[0]; - strm.avail_out = BUFSIZE; + strm.avail_out = buffer.size(); int ret = 0; do { ret = Z(inflate)(&strm, Z_NO_FLUSH); if (strm.avail_out == 0) { const auto off = buffer.size(); - buffer.reserve(off + BUFSIZE); + buffer.resize(off + BUFSIZE); strm.next_out = &buffer[off]; strm.avail_out = BUFSIZE; } diff --git a/src/ZstdDecompressor.cpp b/src/ZstdDecompressor.cpp index 6e2c62f..879aa37 100644 --- a/src/ZstdDecompressor.cpp +++ b/src/ZstdDecompressor.cpp @@ -29,7 +29,8 @@ void ZstdDecompressor::decompress(ustring &buffer) // output space is extended in chunks of this size constexpr size_t BUFSIZE = 8 * 1024; - buffer.resize(BUFSIZE); + if (buffer.empty()) + buffer.resize(BUFSIZE); ZSTD_outBuffer outbuf = { &buffer[0], buffer.size(), 0 }; ZSTD_initDStream(stream);