Skip to content

Commit 9ee1168

Browse files
committed
Merge pull request #354 from redboltz/fix_348
Fixed #348.
2 parents 95e0fc5 + 9282299 commit 9ee1168

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

include/msgpack/unpack.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,13 +1534,16 @@ inline unpacked unpack(
15341534
msgpack::object obj;
15351535
msgpack::unique_ptr<msgpack::zone> z(new msgpack::zone);
15361536
referenced = false;
1537+
std::size_t noff = off;
15371538
unpack_return ret = detail::unpack_imp(
1538-
data, len, off, *z, obj, referenced, f, user_data, limit);
1539+
data, len, noff, *z, obj, referenced, f, user_data, limit);
15391540

15401541
switch(ret) {
15411542
case UNPACK_SUCCESS:
1543+
off = noff;
15421544
return unpacked(obj, msgpack::move(z));
15431545
case UNPACK_EXTRA_BYTES:
1546+
off = noff;
15441547
return unpacked(obj, msgpack::move(z));
15451548
case UNPACK_CONTINUE:
15461549
throw msgpack::insufficient_bytes("insufficient bytes");
@@ -1583,15 +1586,18 @@ inline void unpack(unpacked& result,
15831586
msgpack::object obj;
15841587
msgpack::unique_ptr<msgpack::zone> z(new msgpack::zone);
15851588
referenced = false;
1589+
std::size_t noff = off;
15861590
unpack_return ret = detail::unpack_imp(
1587-
data, len, off, *z, obj, referenced, f, user_data, limit);
1591+
data, len, noff, *z, obj, referenced, f, user_data, limit);
15881592

15891593
switch(ret) {
15901594
case UNPACK_SUCCESS:
1595+
off = noff;
15911596
result.set(obj);
15921597
result.zone() = msgpack::move(z);
15931598
return;
15941599
case UNPACK_EXTRA_BYTES:
1600+
off = noff;
15951601
result.set(obj);
15961602
result.zone() = msgpack::move(z);
15971603
return;
@@ -1635,14 +1641,17 @@ inline msgpack::object unpack(
16351641
unpack_reference_func f, void* user_data, unpack_limit const& limit)
16361642
{
16371643
msgpack::object obj;
1644+
std::size_t noff = off;
16381645
referenced = false;
16391646
unpack_return ret = detail::unpack_imp(
1640-
data, len, off, z, obj, referenced, f, user_data, limit);
1647+
data, len, noff, z, obj, referenced, f, user_data, limit);
16411648

16421649
switch(ret) {
16431650
case UNPACK_SUCCESS:
1651+
off = noff;
16441652
return obj;
16451653
case UNPACK_EXTRA_BYTES:
1654+
off = noff;
16461655
return obj;
16471656
case UNPACK_CONTINUE:
16481657
throw msgpack::insufficient_bytes("insufficient bytes");

src/unpack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,13 @@ msgpack_unpack_next(msgpack_unpacked* result,
618618
return MSGPACK_UNPACK_PARSE_ERROR;
619619
}
620620

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

623622
if(e == 0) {
624623
return MSGPACK_UNPACK_CONTINUE;
625624
}
626625

626+
if(off != NULL) { *off = noff; }
627+
627628
result->data = template_data(&ctx);
628629

629630
return MSGPACK_UNPACK_SUCCESS;

test/pack_unpack.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,58 @@ TEST(unpack, convert_to_object_handle_direct_implicit)
317317
EXPECT_EQ(1, oh.get().as<int>());
318318

319319
}
320+
321+
TEST(unpack, insufficient_bytes_ref)
322+
{
323+
msgpack::sbuffer sbuf;
324+
msgpack::pack(sbuf, 255); // uint8 (2bytes)
325+
326+
std::size_t off = 0;
327+
328+
msgpack::unpacked msg;
329+
try {
330+
msgpack::unpack(msg, sbuf.data(), 1, off);
331+
EXPECT_TRUE(false);
332+
}
333+
catch (msgpack::insufficient_bytes const&) {
334+
EXPECT_TRUE(true);
335+
EXPECT_EQ(off, 0);
336+
}
337+
}
338+
339+
TEST(unpack, insufficient_bytes_object_handle)
340+
{
341+
msgpack::sbuffer sbuf;
342+
msgpack::pack(sbuf, 255); // uint8 (2bytes)
343+
344+
std::size_t off = 0;
345+
346+
msgpack::unpacked msg;
347+
try {
348+
msgpack::object_handle oh(msgpack::unpack(sbuf.data(), 1, off));
349+
EXPECT_TRUE(false);
350+
}
351+
catch (msgpack::insufficient_bytes const&) {
352+
EXPECT_TRUE(true);
353+
EXPECT_EQ(off, 0);
354+
}
355+
}
356+
357+
TEST(unpack, insufficient_bytes_zone)
358+
{
359+
msgpack::sbuffer sbuf;
360+
msgpack::pack(sbuf, 255); // uint8 (2bytes)
361+
362+
std::size_t off = 0;
363+
364+
msgpack::unpacked msg;
365+
try {
366+
msgpack::zone z;
367+
msgpack::unpack(z, sbuf.data(), 1, off);
368+
EXPECT_TRUE(false);
369+
}
370+
catch (msgpack::insufficient_bytes const&) {
371+
EXPECT_TRUE(true);
372+
EXPECT_EQ(off, 0);
373+
}
374+
}

test/pack_unpack_c.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,25 @@ TEST(unpack, sequence)
6868
msgpack_unpacked_destroy(&msg);
6969
}
7070

71+
TEST(pack, insufficient)
72+
{
73+
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
74+
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
75+
76+
EXPECT_EQ(0, msgpack_pack_int(pk, 255)); // uint8 (2bytes)
77+
78+
msgpack_unpack_return success;
79+
size_t offset = 0;
80+
81+
msgpack_unpacked msg;
82+
msgpack_unpacked_init(&msg);
83+
84+
success = msgpack_unpack_next(&msg, sbuf->data, 1, &offset);
85+
EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);
86+
EXPECT_EQ(0, offset);
87+
88+
msgpack_unpacked_destroy(&msg);
89+
90+
msgpack_sbuffer_free(sbuf);
91+
msgpack_packer_free(pk);
92+
}

0 commit comments

Comments
 (0)