Skip to content
Open
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
32 changes: 20 additions & 12 deletions msgpack/pack_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,33 +551,41 @@ static inline int msgpack_pack_ext(msgpack_packer* x, char typecode, size_t l)
*/
static inline int msgpack_pack_timestamp(msgpack_packer* x, int64_t seconds, uint32_t nanoseconds)
{
int ret;
if ((seconds >> 34) == 0) {
/* seconds is unsigned and fits in 34 bits */
uint64_t data64 = ((uint64_t)nanoseconds << 34) | (uint64_t)seconds;
if ((data64 & 0xffffffff00000000L) == 0) {
/* no nanoseconds and seconds is 32bits or smaller. timestamp32. */
unsigned char buf[4];
uint32_t data32 = (uint32_t)data64;
msgpack_pack_ext(x, -1, 4);
ret = msgpack_pack_ext(x, -1, 4);
if (ret != 0)
return ret;

_msgpack_store32(buf, data32);
msgpack_pack_raw_body(x, buf, 4);
return msgpack_pack_raw_body(x, buf, 4);
} else {
/* timestamp64 */
unsigned char buf[8];
msgpack_pack_ext(x, -1, 8);
_msgpack_store64(buf, data64);
msgpack_pack_raw_body(x, buf, 8);
ret = msgpack_pack_ext(x, -1, 8);
if (ret != 0)
return ret;

_msgpack_store64(buf, data64);
return msgpack_pack_raw_body(x, buf, 8);
}
} else {
/* seconds is signed or >34bits */
unsigned char buf[12];
_msgpack_store32(&buf[0], nanoseconds);
_msgpack_store64(&buf[4], seconds);
msgpack_pack_ext(x, -1, 12);
msgpack_pack_raw_body(x, buf, 12);
/* seconds is signed or >34bits */
unsigned char buf[12];
_msgpack_store32(&buf[0], nanoseconds);
_msgpack_store64(&buf[4], seconds);
ret = msgpack_pack_ext(x, -1, 12);
if (ret != 0)
return ret;

return msgpack_pack_raw_body(x, buf, 12);
}
return 0;
}
Comment on lines 578 to 589
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was my code, I would probably prefer doing something like

if (condition) {
  // whatever
  return something;
}

// the contents of the else

to make it clear to the reader that we never not return a value from the function. This is even more relevant now that I've removed the return 0 at the end. But it's not my code so... let me know.



Expand Down
Loading