Skip to content

Commit 3747344

Browse files
authored
Merge pull request DeusData#960 from DeusData/fix/path-within-root-indexable
mcp: make cbm_path_within_root parseable without the preprocessor
2 parents 620179f + be7534b commit 3747344

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
@@ -4363,20 +4363,34 @@ static yyjson_doc *enrich_node_properties(yyjson_mut_doc *doc, yyjson_mut_val *o
43634363
* the search path (attach_result_source) route through it, so a result whose
43644364
* indexed path escapes the project root — via a `..` segment, or a symlink /
43654365
* Windows junction picked up during discovery — is never read back out. */
4366+
/* Canonicalize `path` (resolve symlinks/junctions and `..`) into `out`
4367+
* (>= CBM_SZ_4K bytes); returns true on success. Isolating the per-OS resolver
4368+
* keeps cbm_path_within_root's control flow unconditional: the previous `#ifdef`
4369+
* opened the `if (...) {` brace in one branch and a different one in the other,
4370+
* sharing a single close brace — legal C, but it splits the function's braces
4371+
* across preprocessor branches, which defeats source-level tooling that parses
4372+
* without the preprocessor (and left this function unindexed in the graph). */
4373+
static bool resolve_canonical_path(const char *path, char *out, size_t out_sz) {
4374+
#ifdef _WIN32
4375+
if (!_fullpath(out, path, out_sz)) {
4376+
return false;
4377+
}
4378+
cbm_normalize_path_sep(out);
4379+
return true;
4380+
#else
4381+
(void)out_sz;
4382+
return realpath(path, out) != NULL;
4383+
#endif
4384+
}
4385+
43664386
bool cbm_path_within_root(const char *root_path, const char *abs_path) {
43674387
if (!root_path || !abs_path) {
43684388
return false;
43694389
}
43704390
char real_root[CBM_SZ_4K];
43714391
char real_file[CBM_SZ_4K];
4372-
#ifdef _WIN32
4373-
if (_fullpath(real_root, root_path, sizeof(real_root)) &&
4374-
_fullpath(real_file, abs_path, sizeof(real_file))) {
4375-
cbm_normalize_path_sep(real_root);
4376-
cbm_normalize_path_sep(real_file);
4377-
#else
4378-
if (realpath(root_path, real_root) && realpath(abs_path, real_file)) {
4379-
#endif
4392+
if (resolve_canonical_path(root_path, real_root, sizeof(real_root)) &&
4393+
resolve_canonical_path(abs_path, real_file, sizeof(real_file))) {
43804394
size_t root_len = strlen(real_root);
43814395
if (strncmp(real_file, real_root, root_len) == 0 &&
43824396
(real_file[root_len] == '/' || real_file[root_len] == '\0')) {

0 commit comments

Comments
 (0)