MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / doclink_normalize

Function doclink_normalize

src/pipeline/pass_doclinks.c:135–170  ·  view source on GitHub ↗

Normalize "a/./b/../c" into "a/c". Rejects paths that escape the repo * root (leading ".."), empty results, and over-long/over-deep inputs. */

Source from the content-addressed store, hash-verified

133/* Normalize "a/./b/../c" into "a/c". Rejects paths that escape the repo
134 * root (leading ".."), empty results, and over-long/over-deep inputs. */
135static bool doclink_normalize(const char *in, char *out, size_t out_sz) {
136 size_t seg_starts[DOCLINK_MAX_SEGS];
137 int depth = 0;
138 size_t out_len = 0;
139 const char *p = in;
140 out[0] = '\0';
141 while (*p) {
142 const char *seg = p;
143 const char *slash = strchr(p, '/');
144 size_t seg_len = slash ? (size_t)(slash - p) : strlen(p);
145 p = slash ? slash + SKIP_ONE : p + seg_len;
146 if (seg_len == 0 || (seg_len == SKIP_ONE && seg[0] == '.')) {
147 continue;
148 }
149 if (seg_len == PAIR_LEN && seg[0] == '.' && seg[SKIP_ONE] == '.') {
150 if (depth == 0) {
151 return false; /* escapes the repo root */
152 }
153 depth--;
154 out_len = seg_starts[depth];
155 out[out_len] = '\0';
156 continue;
157 }
158 if (depth >= DOCLINK_MAX_SEGS || out_len + seg_len + PAIR_LEN >= out_sz) {
159 return false;
160 }
161 seg_starts[depth++] = out_len;
162 if (out_len > 0) {
163 out[out_len++] = '/';
164 }
165 memcpy(out + out_len, seg, seg_len);
166 out_len += seg_len;
167 out[out_len] = '\0';
168 }
169 return out_len > 0;
170}
171
172/* One raw match before dedup. Collected in a growable vector (no per-file
173 * cap) and collapsed by doclink_flush(). */

Callers 1

doclink_resolveFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected