Fix zbuffer write of empty string

When a zlib stream has avail_in == 0, it will deflate with a Z_BUF_ERROR
to indicate that "no progress is possible". A zbuffer write with an
empty string will trigger this condition, and the write call
returns/raises an error/exception.  The fix is simple: change the
do/while to a while loop, so that it only tries to deflate if there is
actually data to deflate.
This commit is contained in:
Lawrence Chan 2015-06-22 12:50:32 -05:00
parent 32c42d2f4c
commit addf52e9f0
2 changed files with 4 additions and 4 deletions

View File

@ -131,7 +131,7 @@ int msgpack_zbuffer_write(void* data, const char* buf, size_t len)
zbuf->stream.next_in = (Bytef*)buf; zbuf->stream.next_in = (Bytef*)buf;
zbuf->stream.avail_in = len; zbuf->stream.avail_in = len;
do { while(zbuf->stream.avail_in > 0) {
if(zbuf->stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { if(zbuf->stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
if(!msgpack_zbuffer_expand(zbuf)) { if(!msgpack_zbuffer_expand(zbuf)) {
return -1; return -1;
@ -141,7 +141,7 @@ int msgpack_zbuffer_write(void* data, const char* buf, size_t len)
if(deflate(&zbuf->stream, Z_NO_FLUSH) != Z_OK) { if(deflate(&zbuf->stream, Z_NO_FLUSH) != Z_OK) {
return -1; return -1;
} }
} while(zbuf->stream.avail_in > 0); }
return 0; return 0;
} }

View File

@ -65,7 +65,7 @@ public:
m_stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(buf)); m_stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(buf));
m_stream.avail_in = len; m_stream.avail_in = len;
do { while(m_stream.avail_in > 0) {
if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) {
if(!expand()) { if(!expand()) {
throw std::bad_alloc(); throw std::bad_alloc();
@ -75,7 +75,7 @@ public:
if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) { if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
} while(m_stream.avail_in > 0); }
} }
char* flush() char* flush()