Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(mcp): preserve malformed raw source bytes

Martin Vogel10af29d50c85
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 939aa3226..899a5e0a4 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -12992,10 +12992,55 @@ static bool search_utf8_continuation_byte(unsigned char byte) {
return (byte & 0xC0U) == 0x80U;
}
+/* A continuation-shaped byte is not necessarily part of valid UTF-8. Adjust
+ * paging only when a complete sequence actually spans the requested boundary;
+ * malformed bytes remain independently addressable original source bytes. */
+static size_t search_utf8_page_start(const char *content, size_t total, size_t start) {
+ if (start >= total ||
+ !search_utf8_continuation_byte((unsigned char)content[start])) {
+ return start;
+ }
+ size_t earliest = start > 3U ? start - 3U : 0U;
+ for (size_t candidate = start; candidate > earliest;) {
+ candidate--;
+ if (search_utf8_continuation_byte((unsigned char)content[candidate])) {
+ continue;
+ }
+ size_t sequence = utf8_sequence_len((const unsigned char *)content + candidate,
+ (const unsigned char *)content + total);
+ if (sequence > 1U && candidate + sequence > start) {
+ return candidate + sequence;
+ }
+ break;
+ }
+ return start;
+}
+
+static size_t search_utf8_page_end(const char *content, size_t total, size_t end) {
+ if (end >= total || !search_utf8_continuation_byte((unsigned char)content[end])) {
+ return end;
+ }
+ size_t earliest = end > 3U ? end - 3U : 0U;
+ for (size_t candidate = end; candidate > earliest;) {
+ candidate--;
+ if (search_utf8_continuation_byte((unsigned char)content[candidate])) {
+ continue;
+ }
+ size_t sequence = utf8_sequence_len((const unsigned char *)content + candidate,
+ (const unsigned char *)content + total);
+ if (sequence > 1U && candidate + sequence > end) {
+ return candidate;
+ }
+ break;
+ }
+ return end;
+}
+
/* Select one bounded raw-line preview. By default it contains the complete
* match whenever the match itself fits. An explicit content offset pages the
- * original line independently of the raw-row cursor. Both boundaries are
- * adjusted to UTF-8 code-point boundaries and the adjusted start is reported. */
+ * original line independently of the raw-row cursor. Boundaries inside valid
+ * UTF-8 code points are adjusted and reported; malformed bytes remain exactly
+ * addressable and are reversibly encoded by the shared output boundary. */
static void search_raw_preview(grep_match_t *match, const char *content,
bool raw_content_offset_set, size_t raw_content_offset,
bool match_known, size_t match_start, size_t match_end) {
@@ -13016,23 +13061,17 @@ static void search_raw_preview(grep_match_t *match, const char *content,
}
}
}
- while (start < total && search_utf8_continuation_byte((unsigned char)content[start])) {
- start++;
- }
+ start = search_utf8_page_start(content, total, start);
size_t remaining = total - start;
size_t returned = remaining < preview_capacity ? remaining : preview_capacity;
size_t end = start + returned;
if (end < total) {
- while (end > start && end < total &&
- search_utf8_continuation_byte((unsigned char)content[end])) {
- end--;
- }
+ end = search_utf8_page_end(content, total, end);
}
returned = end - start;
memcpy(match->content, content + start, returned);
match->content[returned] = '\0';
- sanitize_utf8_inplace(match->content);
match->content_start_byte = start;
match->content_returned_bytes = returned;
match->content_total_bytes = total;
@@ -13042,6 +13081,19 @@ static void search_raw_preview(grep_match_t *match, const char *content,
match->content_truncated = start > 0 || end < total;
}
+#ifdef CBM_ENABLE_TEST_SEAMS
+char *cbm_mcp_render_raw_preview_for_testing(const char *content, bool content_offset_set,
+ size_t content_offset, bool json_format) {
+ if (!content) {
+ return NULL;
+ }
+ grep_match_t raw = {.file = "fixture.raw", .line = 7};
+ search_raw_preview(&raw, content, content_offset_set, content_offset, false, 0, 0);
+ return render_search_payload(NULL, 0, &raw, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
+ "", false, false, 0, false, json_format);
+}
+#endif
+
static grep_match_t *collect_grep_matches(FILE *fp, const char *root_path, size_t root_len,
bool has_path_filter, cbm_regex_t *path_regex,
const char *pattern, bool use_regex,
diff --git a/src/mcp/mcp_internal.h b/src/mcp/mcp_internal.h
index bba4305c3..95b733645 100644
--- a/src/mcp/mcp_internal.h
+++ b/src/mcp/mcp_internal.h
@@ -90,6 +90,12 @@ void cbm_mcp_server_set_snapshot_read_test_hook(cbm_mcp_server_t *srv,
char *cbm_mcp_render_search_rows_for_testing(const char *const *qualified_names,
const char *const *file_paths, int row_count,
bool json_format);
+
+/* Drive the production raw-source slicer and renderer without relying on an
+ * external grep/PowerShell text decoder. This keeps malformed-byte paging
+ * deterministic on every test platform. */
+char *cbm_mcp_render_raw_preview_for_testing(const char *content, bool content_offset_set,
+ size_t content_offset, bool json_format);
#endif
#endif
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index ffc65d245..c93cb7fb6 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -8563,6 +8563,73 @@ TEST(search_code_raw_preview_centers_late_match_and_pages_content_bytes) {
PASS();
}
+TEST(search_code_raw_preview_reversibly_pages_malformed_utf8_bytes) {
+ const char malformed[] = {'H', 'E', 'A', 'D', (char)0x80, (char)0xff,
+ 'T', 'A', 'I', 'L', '\0'};
+
+ char *json = cbm_mcp_render_raw_preview_for_testing(malformed, false, 0, true);
+ ASSERT_NOT_NULL(json);
+ yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc); /* The direct JSON payload must remain strict JSON. */
+ yyjson_val *root = yyjson_doc_get_root(doc);
+ yyjson_val *raw = yyjson_obj_get(root, "raw_matches");
+ yyjson_val *rows = raw ? yyjson_obj_get(raw, "rows") : NULL;
+ yyjson_val *row = rows ? yyjson_arr_get(rows, 0) : NULL;
+ ASSERT_NOT_NULL(row);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(row, 2)),
+ "@bytes:4845414480ff5441494c");
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 3)), 0);
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 4)), 10);
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 5)), 10);
+ yyjson_doc_free(doc);
+ free(json);
+
+ /* An explicit byte page beginning on malformed 0x80 must neither skip the
+ * byte nor reinterpret the offset as an encoded-text offset. */
+ json = cbm_mcp_render_raw_preview_for_testing(malformed, true, 4, true);
+ ASSERT_NOT_NULL(json);
+ doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc);
+ root = yyjson_doc_get_root(doc);
+ raw = yyjson_obj_get(root, "raw_matches");
+ rows = raw ? yyjson_obj_get(raw, "rows") : NULL;
+ row = rows ? yyjson_arr_get(rows, 0) : NULL;
+ ASSERT_NOT_NULL(row);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(row, 2)), "@bytes:80ff5441494c");
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 3)), 4);
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 4)), 6);
+ ASSERT_EQ((int)yyjson_get_uint(yyjson_arr_get(row, 5)), 10);
+ yyjson_doc_free(doc);
+ free(json);
+
+ char *tree = cbm_mcp_render_raw_preview_for_testing(malformed, true, 4, false);
+ ASSERT_NOT_NULL(tree);
+ ASSERT_NOT_NULL(strstr(tree, "@bytes:80ff5441494c"));
+ ASSERT_NOT_NULL(strstr(tree, "content_start_byte"));
+ ASSERT_NOT_NULL(strstr(tree, " 4 6 10 "));
+ free(tree);
+
+ /* Literal reserved prefixes remain distinguishable from encoded bytes. */
+ json = cbm_mcp_render_raw_preview_for_testing("@bytes:literal", false, 0, true);
+ ASSERT_NOT_NULL(json);
+ doc = yyjson_read(json, strlen(json), 0);
+ ASSERT_NOT_NULL(doc);
+ root = yyjson_doc_get_root(doc);
+ raw = yyjson_obj_get(root, "raw_matches");
+ rows = raw ? yyjson_obj_get(raw, "rows") : NULL;
+ row = rows ? yyjson_arr_get(rows, 0) : NULL;
+ ASSERT_NOT_NULL(row);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(row, 2)), "@utf8:@bytes:literal");
+ yyjson_doc_free(doc);
+ free(json);
+
+ tree = cbm_mcp_render_raw_preview_for_testing("@bytes:literal", false, 0, false);
+ ASSERT_NOT_NULL(tree);
+ ASSERT_NOT_NULL(strstr(tree, "@utf8:@bytes:literal"));
+ free(tree);
+ PASS();
+}
+
TEST(search_code_match_locations_are_explicitly_bounded_and_expandable) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_match_locations_XXXXXX");
@@ -18896,6 +18963,7 @@ SUITE(mcp) {
RUN_TEST(search_code_preserves_multi_kib_identities_and_distinct_long_directories);
RUN_TEST(search_code_long_raw_line_is_one_truthfully_truncated_match);
RUN_TEST(search_code_raw_preview_centers_late_match_and_pages_content_bytes);
+ RUN_TEST(search_code_raw_preview_reversibly_pages_malformed_utf8_bytes);
RUN_TEST(search_code_match_locations_are_explicitly_bounded_and_expandable);
RUN_TEST(search_code_scoped_path_with_spaces_issue687);
#ifdef _WIN32