Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ixwebsocket/IXGzipCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,23 @@ namespace ix
const int kBufferSize = 1 << 14;
std::array<unsigned char, kBufferSize> compressBuffer;

int ret;
do
{
inflateState.avail_out = (uInt) kBufferSize;
inflateState.next_out = &compressBuffer.front();

int ret = inflate(&inflateState, Z_SYNC_FLUSH);
ret = inflate(&inflateState, Z_NO_FLUSH);

if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
if (ret != Z_OK && ret != Z_STREAM_END)
{
inflateEnd(&inflateState);
return false;
}

out.append(reinterpret_cast<char*>(&compressBuffer.front()),
kBufferSize - inflateState.avail_out);
} while (inflateState.avail_out == 0);
} while (ret != Z_STREAM_END);

inflateEnd(&inflateState);
return true;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ endif()

if (USE_ZLIB)
list(APPEND TEST_TARGET_NAMES
IXGzipCodecTest
IXWebSocketPerMessageDeflateCompressorTest
)
endif()
Expand Down
34 changes: 34 additions & 0 deletions test/IXGzipCodecTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* IXGzipCodecTest.cpp
* Copyright (c) 2026 Machine Zone, Inc. All rights reserved.
*/
#include <catch_amalgamated.hpp>
#include <ixwebsocket/IXGzipCodec.h>

TEST_CASE("Gzip decoding requires a complete stream", "[gzip]")
{
const std::string body = GENERATE(std::string(), std::string("hello"), std::string(32768, 'x'));
const std::string encoded = ix::gzipCompress(body);
REQUIRE_FALSE(encoded.empty());
for (size_t length = 0; length < encoded.size(); ++length)
{
CAPTURE(length, encoded.size());
std::string decoded;
CHECK_FALSE(ix::gzipDecompress(encoded.substr(0, length), decoded));
}
std::string decoded;
REQUIRE(ix::gzipDecompress(encoded, decoded));
CHECK(decoded == body);
}

TEST_CASE("Gzip decoding checks integrity and preserves append behavior", "[gzip]")
{
const std::string body("hello\0world", 11);
auto encoded = ix::gzipCompress(body);
std::string decoded = "prefix";
REQUIRE(ix::gzipDecompress(encoded, decoded));
CHECK(decoded == "prefix" + body);
encoded[encoded.size() - 8] ^= 1;
decoded.clear();
CHECK_FALSE(ix::gzipDecompress(encoded, decoded));
}
Loading