From addf52e9f01beb1a5ec224aad35ee4bf3a2a506f Mon Sep 17 00:00:00 2001 From: Lawrence Chan Date: Mon, 22 Jun 2015 12:50:32 -0500 Subject: [PATCH] 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. --- include/msgpack/zbuffer.h | 4 ++-- include/msgpack/zbuffer.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/msgpack/zbuffer.h b/include/msgpack/zbuffer.h index dbad72c5..dbad8591 100644 --- a/include/msgpack/zbuffer.h +++ b/include/msgpack/zbuffer.h @@ -131,7 +131,7 @@ int msgpack_zbuffer_write(void* data, const char* buf, size_t len) zbuf->stream.next_in = (Bytef*)buf; zbuf->stream.avail_in = len; - do { + while(zbuf->stream.avail_in > 0) { if(zbuf->stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { if(!msgpack_zbuffer_expand(zbuf)) { 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) { return -1; } - } while(zbuf->stream.avail_in > 0); + } return 0; } diff --git a/include/msgpack/zbuffer.hpp b/include/msgpack/zbuffer.hpp index 688a70b5..71880648 100644 --- a/include/msgpack/zbuffer.hpp +++ b/include/msgpack/zbuffer.hpp @@ -65,7 +65,7 @@ public: m_stream.next_in = reinterpret_cast(const_cast(buf)); m_stream.avail_in = len; - do { + while(m_stream.avail_in > 0) { if(m_stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { if(!expand()) { throw std::bad_alloc(); @@ -75,7 +75,7 @@ public: if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) { throw std::bad_alloc(); } - } while(m_stream.avail_in > 0); + } } char* flush()