Skip to content

mcp: make cbm_path_within_root parseable without the preprocessor#960

Open
DeusData wants to merge 1 commit into
mainfrom
fix/path-within-root-indexable
Open

mcp: make cbm_path_within_root parseable without the preprocessor#960
DeusData wants to merge 1 commit into
mainfrom
fix/path-within-root-indexable

Conversation

@DeusData

@DeusData DeusData commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Problem

cbm_path_within_root (the containment guard every MCP file-read sink passes through) was missing from the code knowledge graph — despite its callers being present. Root cause: its definition used

#ifdef _WIN32
    if (_fullpath(...) && _fullpath(...)) {   // opening brace in the WIN32 branch
        ...
#else
    if (realpath(...) && realpath(...)) {     // a different opening brace in the POSIX branch
#endif
        ...shared body...
    }                                         // single shared close brace

That's legal C, but the #ifdef splits the function body's braces across preprocessor branches. A source-level parser that runs without the preprocessor (the tree-sitter-based indexer) sees two if { opens and one } close → unbalanced → error node → no Function node emitted.

Change

Extract a tiny per-OS resolve_canonical_path() helper (each #ifdef branch is now a complete statement), leaving cbm_path_within_root with one unconditional if. Purely structural — behavior is identical.

Verification

  • mcp_path_within_root_rejects_escape and path_within_root_allowed still pass; full C build + lint clean.
  • Re-indexed the tree: cbm_path_within_root now appears as a Function node with in_degree=6 (all callers resolve to it), and the new resolve_canonical_path helper is linked. Before this change it was absent from the graph entirely.

The general extractor limitation (functions whose braces are split by #ifdef) is tracked separately as an issue; this PR fixes the one security-relevant function and removes a genuine code smell.

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>
@DeusData DeusData added bug Something isn't working parsing/quality Graph extraction bugs, false positives, missing edges labels Jul 8, 2026
@DeusData DeusData added this to the 0.9.1-rc milestone Jul 8, 2026
@DeusData DeusData added security Security vulnerabilities, hardening priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. labels Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parsing/quality Graph extraction bugs, false positives, missing edges priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. security Security vulnerabilities, hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant