fix(foundation): properly escape JSON control characters as \u00XX#527
fix(foundation): properly escape JSON control characters as \u00XX#527Ravandevil25 wants to merge 1 commit into
Conversation
Signed-off-by: Saurav Kumar <sauravsk2507@gmail.com>
|
Thanks @Ravandevil25 — the control-character |
|
Thanks @Ravandevil25 — the
One note: the pipeline escapers in |
|
Correction to my earlier note — apologies, that was my mistake. I'd checked The one remaining item before merge is the reproduce-first test: assert that a control byte (e.g. Thanks for the fix, and sorry for the confusion. 🙏 |
Describe the bug
In
src/foundation/str_util.c, thecbm_json_escapefunction was handling control characters< 0x20by completely skipping them instead of properly escaping them as\u00XX. Thejson_escaped_lenfunction mirrored this logic. While this avoided buffer overflows, it silently stripped control characters, resulting in an invalid JSON representation of the actual data.Impact
Silently stripping control characters from strings mutates the data. If a file path, git branch, or code snippet contained unusual control characters, they were silently erased from the MCP JSON response rather than safely serialized.
Fix
cbm_json_escapeto safely format control characters as\u00XXusingsnprintf(buf + pos, 7, "\\u%04x", c).#include <stdio.h>tostr_util.cforsnprintf.json_escaped_lento correctly reserve 6 bytes (len += 6) for unhandled control characters, aligning the buffer sizing with the formatting string.Local tests and strict compilation (
-Wall -Werror) pass successfully.