Skip to content

Commit 1a92b01

Browse files
authored
Merge pull request #1235 from rajath201/cpp-zbuffer-uint-counters
split long writes across zlib's 32-bit counters in C++ zbuffer
2 parents 0105f53 + f88e19e commit 1a92b01

3 files changed

Lines changed: 62 additions & 10 deletions

File tree

‎include/msgpack/v1/zbuffer.hpp‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "msgpack/v1/zbuffer_decl.hpp"
1414
#include "msgpack/assert.hpp"
1515

16+
#include <limits>
1617
#include <stdexcept>
1718
#include <zlib.h>
1819

@@ -51,19 +52,26 @@ class zbuffer {
5152
if (!buf) return;
5253

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

56-
while(m_stream.avail_in > 0) {
57-
if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
58-
if(!expand()) {
59-
throw std::bad_alloc();
56+
// avail_in is narrower than size_t on 64-bit platforms
57+
const size_t max_avail = std::numeric_limits<uInt>::max();
58+
do {
59+
uInt chunk = (len > max_avail) ? std::numeric_limits<uInt>::max() : static_cast<uInt>(len);
60+
m_stream.avail_in = chunk;
61+
len -= chunk;
62+
63+
while(m_stream.avail_in > 0) {
64+
if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
65+
if(!expand()) {
66+
throw std::bad_alloc();
67+
}
6068
}
61-
}
6269

63-
if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
64-
throw std::bad_alloc();
70+
if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
71+
throw std::bad_alloc();
72+
}
6573
}
66-
}
74+
} while(len > 0);
6775
}
6876

6977
char* flush()
@@ -139,9 +147,13 @@ class zbuffer {
139147
return false;
140148
}
141149

150+
const size_t avail = nsize - used;
151+
const size_t max_avail = std::numeric_limits<uInt>::max();
152+
142153
m_data = tmp;
143154
m_stream.next_out = reinterpret_cast<Bytef*>(tmp + used);
144-
m_stream.avail_out = static_cast<uInt>(nsize - used);
155+
// avail_out is narrower than size_t on 64-bit platforms
156+
m_stream.avail_out = (avail > max_avail) ? std::numeric_limits<uInt>::max() : static_cast<uInt>(avail);
145157

146158
return true;
147159
}

‎test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ LIST (APPEND check_PROGRAMS
3131
user_class.cpp
3232
version.cpp
3333
visitor.cpp
34+
zbuffer_sizes.cpp
3435
zone.cpp
3536
)
3637

‎test/zbuffer_sizes.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <msgpack.hpp>
2+
#include <zlib.h>
3+
4+
// A length over 4 GiB can't go through the real deflate() in a unit test: it
5+
// would read all of it. This stand-in only counts the input it is given.
6+
static unsigned long long deflated_bytes = 0;
7+
8+
static int count_deflated_bytes(z_streamp strm, int /* flush */)
9+
{
10+
deflated_bytes += strm->avail_in;
11+
strm->avail_in = 0;
12+
return Z_OK;
13+
}
14+
15+
#define deflate count_deflated_bytes
16+
#include <msgpack/zbuffer.hpp>
17+
#undef deflate
18+
19+
#define BOOST_TEST_MODULE zbuffer_sizes
20+
#include <boost/test/unit_test.hpp>
21+
22+
#include <climits>
23+
#include <cstdint>
24+
25+
// Needs a size_t wider than zlib's uInt. Guarded with the preprocessor rather
26+
// than a runtime check so that the constant does not overflow (and warn) on
27+
// 32-bit targets.
28+
BOOST_AUTO_TEST_CASE(write_length_wider_than_avail_in)
29+
{
30+
#if SIZE_MAX > UINT_MAX
31+
const size_t len = static_cast<size_t>(UINT_MAX) + 2;
32+
const char byte = 0;
33+
msgpack::zbuffer zbuf(1);
34+
35+
deflated_bytes = 0;
36+
zbuf.write(&byte, len);
37+
BOOST_CHECK_EQUAL(static_cast<unsigned long long>(len), deflated_bytes);
38+
#endif // SIZE_MAX > UINT_MAX
39+
}

0 commit comments

Comments
 (0)