Skip to content

fix: properly handle return codes in pack_timestamp - #672

Merged
methane merged 1 commit into
msgpack:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-return-codes-in-pack_timestamp
May 23, 2026
Merged

methane merged 1 commit into
msgpack:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-return-codes-in-pack_timestamp

Conversation

@KowalskiThomas

@KowalskiThomas KowalskiThomas commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

What is this PR?

This PR makes it so that return codes of helpers called in msgpack_pack_ext are properly checked and surfaced if they're indicative of a failure.

Note the "big" diff at the end of the function is an indentation change on top of the actual change -- the code used to be indented with three spaces instead of four. I changed that (should be clear looking at the diff without whitespace changes).

Comment thread msgpack/pack_template.h
Comment on lines 578 to 589
} 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;
}

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.

@KowalskiThomas
KowalskiThomas marked this pull request as ready for review May 11, 2026 14:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR ensures msgpack_pack_timestamp() correctly propagates failures from the underlying packing helpers, so callers can observe and handle allocation/write errors instead of silently succeeding.

Changes:

  • Capture and check the return value from msgpack_pack_ext() in all timestamp encoding branches.
  • Propagate the return value from msgpack_pack_raw_body() (instead of always returning 0).
  • Normalize indentation in the timestamp96 branch to match the surrounding style.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@methane
methane merged commit 378edc6 into msgpack:main May 23, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants