Skip to content
Open
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
32 changes: 22 additions & 10 deletions include/msgpack/v1/zbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "msgpack/v1/zbuffer_decl.hpp"
#include "msgpack/assert.hpp"

#include <limits>
#include <stdexcept>
#include <zlib.h>

Expand Down Expand Up @@ -51,19 +52,26 @@ class zbuffer {
if (!buf) return;

m_stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(buf));
m_stream.avail_in = static_cast<uInt>(len);

while(m_stream.avail_in > 0) {
if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
if(!expand()) {
throw std::bad_alloc();
// avail_in is narrower than size_t on 64-bit platforms
const size_t max_avail = std::numeric_limits<uInt>::max();
do {
uInt chunk = (len > max_avail) ? std::numeric_limits<uInt>::max() : static_cast<uInt>(len);
m_stream.avail_in = chunk;
len -= chunk;

while(m_stream.avail_in > 0) {
if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
if(!expand()) {
throw std::bad_alloc();
}
}
}

if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
throw std::bad_alloc();
if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
throw std::bad_alloc();
}
}
}
} while(len > 0);
}

char* flush()
Expand Down Expand Up @@ -139,9 +147,13 @@ class zbuffer {
return false;
}

const size_t avail = nsize - used;
const size_t max_avail = std::numeric_limits<uInt>::max();

m_data = tmp;
m_stream.next_out = reinterpret_cast<Bytef*>(tmp + used);
m_stream.avail_out = static_cast<uInt>(nsize - used);
// avail_out is narrower than size_t on 64-bit platforms
m_stream.avail_out = (avail > max_avail) ? std::numeric_limits<uInt>::max() : static_cast<uInt>(avail);

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 @@ -31,6 +31,7 @@ LIST (APPEND check_PROGRAMS
user_class.cpp
version.cpp
visitor.cpp
zbuffer_sizes.cpp
zone.cpp
)

Expand Down
39 changes: 39 additions & 0 deletions test/zbuffer_sizes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <msgpack.hpp>
#include <zlib.h>

// A length over 4 GiB can't go through the real deflate() in a unit test: it
// would read all of it. This stand-in only counts the input it is given.
static unsigned long long deflated_bytes = 0;

static int count_deflated_bytes(z_streamp strm, int /* flush */)
{
deflated_bytes += strm->avail_in;
strm->avail_in = 0;
return Z_OK;
}

#define deflate count_deflated_bytes
#include <msgpack/zbuffer.hpp>
#undef deflate

#define BOOST_TEST_MODULE zbuffer_sizes
#include <boost/test/unit_test.hpp>

#include <climits>
#include <cstdint>

// Needs a size_t wider than zlib's uInt. Guarded with the preprocessor rather
// than a runtime check so that the constant does not overflow (and warn) on
// 32-bit targets.
BOOST_AUTO_TEST_CASE(write_length_wider_than_avail_in)
{
#if SIZE_MAX > UINT_MAX
const size_t len = static_cast<size_t>(UINT_MAX) + 2;
const char byte = 0;
msgpack::zbuffer zbuf(1);

deflated_bytes = 0;
zbuf.write(&byte, len);
BOOST_CHECK_EQUAL(static_cast<unsigned long long>(len), deflated_bytes);
#endif // SIZE_MAX > UINT_MAX
}
Loading