Skip to content

Commit be7534b

Browse files
committed
mcp: make cbm_path_within_root parseable without the preprocessor
cbm_path_within_root used an #ifdef _WIN32/#else that opened the if(...) { brace in each branch and shared a single close brace. That is legal C, but it splits the function body's braces across preprocessor branches, so a source-level parser running without the preprocessor (e.g. the tree-sitter code indexer) sees unbalanced braces, emits an error node, and never creates a Function node for it — the definition went missing from the graph while its callers stayed linked to an unresolved name. Extract a small per-OS resolve_canonical_path() helper so the function has one unconditional control flow. Behavior is unchanged (the path-containment security tests still pass); it now indexes correctly. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent ee68144 commit be7534b

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,20 +4103,34 @@ static yyjson_doc *enrich_node_properties(yyjson_mut_doc *doc, yyjson_mut_val *o
41034103
* the search path (attach_result_source) route through it, so a result whose
41044104
* indexed path escapes the project root — via a `..` segment, or a symlink /
41054105
* Windows junction picked up during discovery — is never read back out. */
4106+
/* Canonicalize `path` (resolve symlinks/junctions and `..`) into `out`
4107+
* (>= CBM_SZ_4K bytes); returns true on success. Isolating the per-OS resolver
4108+
* keeps cbm_path_within_root's control flow unconditional: the previous `#ifdef`
4109+
* opened the `if (...) {` brace in one branch and a different one in the other,
4110+
* sharing a single close brace — legal C, but it splits the function's braces
4111+
* across preprocessor branches, which defeats source-level tooling that parses
4112+
* without the preprocessor (and left this function unindexed in the graph). */
4113+
static bool resolve_canonical_path(const char *path, char *out, size_t out_sz) {
4114+
#ifdef _WIN32
4115+
if (!_fullpath(out, path, out_sz)) {
4116+
return false;
4117+
}
4118+
cbm_normalize_path_sep(out);
4119+
return true;
4120+
#else
4121+
(void)out_sz;
4122+
return realpath(path, out) != NULL;
4123+
#endif
4124+
}
4125+
41064126
bool cbm_path_within_root(const char *root_path, const char *abs_path) {
41074127
if (!root_path || !abs_path) {
41084128
return false;
41094129
}
41104130
char real_root[CBM_SZ_4K];
41114131
char real_file[CBM_SZ_4K];
4112-
#ifdef _WIN32
4113-
if (_fullpath(real_root, root_path, sizeof(real_root)) &&
4114-
_fullpath(real_file, abs_path, sizeof(real_file))) {
4115-
cbm_normalize_path_sep(real_root);
4116-
cbm_normalize_path_sep(real_file);
4117-
#else
4118-
if (realpath(root_path, real_root) && realpath(abs_path, real_file)) {
4119-
#endif
4132+
if (resolve_canonical_path(root_path, real_root, sizeof(real_root)) &&
4133+
resolve_canonical_path(abs_path, real_file, sizeof(real_file))) {
41204134
size_t root_len = strlen(real_root);
41214135
if (strncmp(real_file, real_root, root_len) == 0 &&
41224136
(real_file[root_len] == '/' || real_file[root_len] == '\0')) {

0 commit comments

Comments
 (0)