diff --git a/include/msgpack/v1/zbuffer.hpp b/include/msgpack/v1/zbuffer.hpp index e59e9da89..65a5f110f 100644 --- a/include/msgpack/v1/zbuffer.hpp +++ b/include/msgpack/v1/zbuffer.hpp @@ -13,6 +13,7 @@ #include "msgpack/v1/zbuffer_decl.hpp" #include "msgpack/assert.hpp" +#include #include #include @@ -51,19 +52,26 @@ class zbuffer { if (!buf) return; m_stream.next_in = reinterpret_cast(const_cast(buf)); - m_stream.avail_in = static_cast(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::max(); + do { + uInt chunk = (len > max_avail) ? std::numeric_limits::max() : static_cast(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() @@ -139,9 +147,13 @@ class zbuffer { return false; } + const size_t avail = nsize - used; + const size_t max_avail = std::numeric_limits::max(); + m_data = tmp; m_stream.next_out = reinterpret_cast(tmp + used); - m_stream.avail_out = static_cast(nsize - used); + // avail_out is narrower than size_t on 64-bit platforms + m_stream.avail_out = (avail > max_avail) ? std::numeric_limits::max() : static_cast(avail); return true; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7a9113ac7..658e694bd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,7 @@ LIST (APPEND check_PROGRAMS user_class.cpp version.cpp visitor.cpp + zbuffer_sizes.cpp zone.cpp ) diff --git a/test/zbuffer_sizes.cpp b/test/zbuffer_sizes.cpp new file mode 100644 index 000000000..cf7803f9a --- /dev/null +++ b/test/zbuffer_sizes.cpp @@ -0,0 +1,39 @@ +#include +#include + +// 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 +#undef deflate + +#define BOOST_TEST_MODULE zbuffer_sizes +#include + +#include +#include + +// 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(UINT_MAX) + 2; + const char byte = 0; + msgpack::zbuffer zbuf(1); + + deflated_bytes = 0; + zbuf.write(&byte, len); + BOOST_CHECK_EQUAL(static_cast(len), deflated_bytes); +#endif // SIZE_MAX > UINT_MAX +}