fix(mcp): return valid UTF-8 snippets#526
Open
jstar0 wants to merge 1 commit into
Open
Conversation
Signed-off-by: King Star <mcxin.y@gmail.com>
Owner
|
Thanks @jstar0 — your reproduce-first test here (write known-bad CP949 bytes, assert the response is valid JSON with U+FFFD replacements) is excellent and we want to keep it. There's a parallel PR #541 fixing the same issue (#511) with a cleaner sanitizer (canonical One change needed here regardless: the overflow guard Would you be open to combining your test with #541's sanitizer? 🙏 |
piiiico
added a commit
to piiiico/codebase-memory-mcp
that referenced
this pull request
Jun 22, 2026
…f8 (DeusData#511) Per maintainer review on DeusData#541, two additions to the snippet UTF-8 sanitizer: 1. **Reproduce-first test** (`snippet_source_invalid_utf8`) Writes a Go source file containing the CP949 sequence 0xC0 0xD4 0xB7 0xC2 inside a comment, then calls `get_code_snippet` and asserts: - the outer MCP envelope parses as strict JSON, - the embedded snippet JSON parses as strict JSON, - the original invalid bytes (0xC0 0xD4) do not appear verbatim, - surrounding valid bytes (HandleRequest, return nil) survive, - U+FFFD (EF BF BD) marks where invalid bytes were. Without `sanitize_utf8`, the response contains raw non-UTF-8 bytes and step 1 fails (yyjson rejects it) — i.e. the test fails on pre-fix `main`. Byte pattern and structure borrowed from DeusData#526 (jstar0) so both contributions converge on the same regression. 2. **Allocation overflow guard** `malloc(src_len * 3 + 1)` is technically UB if `src_len > (SIZE_MAX - 1) / 3`. Snippet sources can't reach that in practice, but the guard makes the safety property local to the function rather than relying on call-site bounds. The same bound is enforced in DeusData#526. Refs DeusData#511. Co-authored-by: King Star <54024410+jstar0@users.noreply.github.com> Signed-off-by: piiiico <pico@amdal.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_code_snippetsource text to valid UTF-8 before serializing the MCP response.Fixes #511.
Changes
sourcefield inbuild_snippet_response().Verification
make -f Makefile.cbm test make -f Makefile.cbm lint-format make -f Makefile.cbm lint-no-suppress git diff --checkI also attempted:
That local run stopped at
cppcheckbecausecppcheckis not installed in my environment.