Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/msgpack/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ static inline bool msgpack_object_to_timestamp(const msgpack_object* obj, msgpac
case 8: {
uint64_t value;
_msgpack_load64(uint64_t, obj->via.ext.ptr, &value);
if ((value >> 34) > 999999999) return false;
ts->tv_nsec = (uint32_t)(value >> 34);
ts->tv_sec = value & 0x00000003ffffffffLL;
return true;
}
case 12:
_msgpack_load32(uint32_t, obj->via.ext.ptr, &ts->tv_nsec);
case 12: {
uint32_t nsec;
_msgpack_load32(uint32_t, obj->via.ext.ptr, &nsec);
if (nsec > 999999999) return false;
ts->tv_nsec = nsec;
_msgpack_load64(int64_t, obj->via.ext.ptr + 4, &ts->tv_sec);
return true;
}
default:
return false;
}
Expand Down
34 changes: 34 additions & 0 deletions test/msgpack_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,40 @@ TEST(MSGPACKC, simple_buffer_timestamp_96)
msgpack_sbuffer_destroy(&sbuf);
}

TEST(MSGPACKC, timestamp_nsec_out_of_range)
{
// timestamp 64 with nanoseconds 1000000000
const char ts64[] = {
(char)0xd7, (char)0xff,
(char)0xee, (char)0x6b, (char)0x28, (char)0x00,
(char)0x00, (char)0x00, (char)0x00, (char)0x00
};
// timestamp 96 with nanoseconds 0xffffffff and seconds -1
const char ts96[] = {
(char)0xc7, (char)0x0c, (char)0xff,
(char)0xff, (char)0xff, (char)0xff, (char)0xff,
(char)0xff, (char)0xff, (char)0xff, (char)0xff,
(char)0xff, (char)0xff, (char)0xff, (char)0xff
};
msgpack_zone z;
msgpack_zone_init(&z, 2048);
msgpack_object obj;
msgpack_timestamp ts;

msgpack_unpack_return ret =
msgpack_unpack(ts64, sizeof(ts64), NULL, &z, &obj);
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
EXPECT_EQ(8u, obj.via.ext.size);
EXPECT_FALSE(msgpack_object_to_timestamp(&obj, &ts));

ret = msgpack_unpack(ts96, sizeof(ts96), NULL, &z, &obj);
EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, ret);
EXPECT_EQ(12u, obj.via.ext.size);
EXPECT_FALSE(msgpack_object_to_timestamp(&obj, &ts));

msgpack_zone_destroy(&z);
}

TEST(MSGPACKC, simple_buffer_array)
{
unsigned int array_size = 5;
Expand Down
Loading