Skip to content

nul-terminate bin/ext output in msgpack_object_print_buffer - #1196

Merged
redboltz merged 1 commit into
msgpack:c_masterfrom
rajath201:object-bin-print-buffer-nul
Oct 28, 2026
Merged

redboltz merged 1 commit into
msgpack:c_masterfrom
rajath201:object-bin-print-buffer-nul

Conversation

@rajath201

Copy link
Copy Markdown

msgpack_object_bin_print_buffer copies printable bin/ext bytes with memcpy and never writes a terminator, unlike the snprintf calls around it. When a payload fills the buffer, msgpack_object_print_buffer returns 0 with no NUL anywhere in it, so a caller that logs the result with %s (as example/user_buffer_unpack.c does) reads past the end. A 6-byte bin8 "test" off the wire printed into a 5-byte buffer trips ASan with a stack-buffer-overflow in strlen.

Keep one byte back in the printable branch and terminate on truncation, the same contract every other type already gets from snprintf. Return values don't change for any input. The cost is one extra byte store per character, nothing measurable. The new object_bin_print_buffer_overflow_terminated test fails on the current tree and passes with the fix, and ctest is 6/6 under ASan/UBSan.

msgpack_object_bin_print_buffer copied printable bytes with memcpy and
never wrote a terminator, unlike the snprintf calls around it. When a
bin or ext payload filled the buffer, msgpack_object_print_buffer
returned 0 with no NUL anywhere in the buffer, so a caller printing it
with %s (as example/user_buffer_unpack.c does) read past the end.

Stop one byte short and terminate the buffer, matching snprintf
truncation. Return values are unchanged.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.74%. Comparing base (eabfe29) to head (77dd4c1).
⚠️ Report is 4 commits behind head on c_master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##           c_master    #1196      +/-   ##
============================================
+ Coverage     57.47%   58.74%   +1.26%     
============================================
  Files            10       10              
  Lines          1124     1144      +20     
  Branches        129      140      +11     
============================================
+ Hits            646      672      +26     
+ Misses          478      472       -6     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@redboltz

Copy link
Copy Markdown
Contributor

@rajath201 thank you for sending the PR. It looks good to me.

@redboltz
redboltz merged commit 8a34070 into msgpack:c_master Oct 28, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants