Skip to content

Commit

Permalink
Merge pull request #354 from redboltz/fix_348
Browse files Browse the repository at this point in the history
Fixed #348.
  • Loading branch information
redboltz committed Aug 26, 2015
2 parents 95e0fc5 + 9282299 commit 9ee1168
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
15 changes: 12 additions & 3 deletions include/msgpack/unpack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,13 +1534,16 @@ inline unpacked unpack(
msgpack::object obj;
msgpack::unique_ptr<msgpack::zone> z(new msgpack::zone);
referenced = false;
std::size_t noff = off;
unpack_return ret = detail::unpack_imp(
data, len, off, *z, obj, referenced, f, user_data, limit);
data, len, noff, *z, obj, referenced, f, user_data, limit);

switch(ret) {
case UNPACK_SUCCESS:
off = noff;
return unpacked(obj, msgpack::move(z));
case UNPACK_EXTRA_BYTES:
off = noff;
return unpacked(obj, msgpack::move(z));
case UNPACK_CONTINUE:
throw msgpack::insufficient_bytes("insufficient bytes");
Expand Down Expand Up @@ -1583,15 +1586,18 @@ inline void unpack(unpacked& result,
msgpack::object obj;
msgpack::unique_ptr<msgpack::zone> z(new msgpack::zone);
referenced = false;
std::size_t noff = off;
unpack_return ret = detail::unpack_imp(
data, len, off, *z, obj, referenced, f, user_data, limit);
data, len, noff, *z, obj, referenced, f, user_data, limit);

switch(ret) {
case UNPACK_SUCCESS:
off = noff;
result.set(obj);
result.zone() = msgpack::move(z);
return;
case UNPACK_EXTRA_BYTES:
off = noff;
result.set(obj);
result.zone() = msgpack::move(z);
return;
Expand Down Expand Up @@ -1635,14 +1641,17 @@ inline msgpack::object unpack(
unpack_reference_func f, void* user_data, unpack_limit const& limit)
{
msgpack::object obj;
std::size_t noff = off;
referenced = false;
unpack_return ret = detail::unpack_imp(
data, len, off, z, obj, referenced, f, user_data, limit);
data, len, noff, z, obj, referenced, f, user_data, limit);

switch(ret) {
case UNPACK_SUCCESS:
off = noff;
return obj;
case UNPACK_EXTRA_BYTES:
off = noff;
return obj;
case UNPACK_CONTINUE:
throw msgpack::insufficient_bytes("insufficient bytes");
Expand Down
3 changes: 2 additions & 1 deletion src/unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,13 @@ msgpack_unpack_next(msgpack_unpacked* result,
return MSGPACK_UNPACK_PARSE_ERROR;
}

if(off != NULL) { *off = noff; }

if(e == 0) {
return MSGPACK_UNPACK_CONTINUE;
}

if(off != NULL) { *off = noff; }

result->data = template_data(&ctx);

return MSGPACK_UNPACK_SUCCESS;
Expand Down
55 changes: 55 additions & 0 deletions test/pack_unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,58 @@ TEST(unpack, convert_to_object_handle_direct_implicit)
EXPECT_EQ(1, oh.get().as<int>());

}

TEST(unpack, insufficient_bytes_ref)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 255); // uint8 (2bytes)

std::size_t off = 0;

msgpack::unpacked msg;
try {
msgpack::unpack(msg, sbuf.data(), 1, off);
EXPECT_TRUE(false);
}
catch (msgpack::insufficient_bytes const&) {
EXPECT_TRUE(true);
EXPECT_EQ(off, 0);
}
}

TEST(unpack, insufficient_bytes_object_handle)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 255); // uint8 (2bytes)

std::size_t off = 0;

msgpack::unpacked msg;
try {
msgpack::object_handle oh(msgpack::unpack(sbuf.data(), 1, off));
EXPECT_TRUE(false);
}
catch (msgpack::insufficient_bytes const&) {
EXPECT_TRUE(true);
EXPECT_EQ(off, 0);
}
}

TEST(unpack, insufficient_bytes_zone)
{
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, 255); // uint8 (2bytes)

std::size_t off = 0;

msgpack::unpacked msg;
try {
msgpack::zone z;
msgpack::unpack(z, sbuf.data(), 1, off);
EXPECT_TRUE(false);
}
catch (msgpack::insufficient_bytes const&) {
EXPECT_TRUE(true);
EXPECT_EQ(off, 0);
}
}
22 changes: 22 additions & 0 deletions test/pack_unpack_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ TEST(unpack, sequence)
msgpack_unpacked_destroy(&msg);
}

TEST(pack, insufficient)
{
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

EXPECT_EQ(0, msgpack_pack_int(pk, 255)); // uint8 (2bytes)

msgpack_unpack_return success;
size_t offset = 0;

msgpack_unpacked msg;
msgpack_unpacked_init(&msg);

success = msgpack_unpack_next(&msg, sbuf->data, 1, &offset);
EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);
EXPECT_EQ(0, offset);

msgpack_unpacked_destroy(&msg);

msgpack_sbuffer_free(sbuf);
msgpack_packer_free(pk);
}

0 comments on commit 9ee1168

Please sign in to comment.