Skip to content

Commit 7814dac

Browse files
committed
fix: size the compress path with size_t and prune the cache from the walk
Two loose ends from the bounds work. The decompression side already took size_t so a >2 GiB capacity could not wrap through int, but compression still took int for both the source length and the destination capacity, and the artifact export cast a size_t database size down to reach it. A database past 2 GiB would have handed the encoder a negative length. Both lengths and the bound helper are size_t now, and the function returns int64_t like its decompressing counterpart. The discovery walk now prunes the cache directory by absolute path. A custom CBM_CACHE_DIR may sit inside a repository — tests do it routinely — and walking into it pulls every other project's graph database into this project's file list. This is the narrow form of a concern that was briefly implemented as refusing any root that contained the cache; refusing a whole root was too blunt, and not walking the cache is what the concern actually asks for. The test fails without the prune: a .go file planted under the cache is otherwise discovered, and "cache" is not in the built-in skip list, so the assertion is not vacuous. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0d6f26a commit 7814dac

5 files changed

Lines changed: 79 additions & 11 deletions

File tree

‎internal/cbm/zstd_store.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include <stddef.h>
88
#include <stdint.h>
99

10-
int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int level) {
11-
size_t rc = ZSTD_compress(dst, (size_t)dstCap, src, (size_t)srcLen, level);
10+
int64_t cbm_zstd_compress(const char *src, size_t srcLen, char *dst, size_t dstCap, int level) {
11+
size_t rc = ZSTD_compress(dst, dstCap, src, srcLen, level);
1212
if (ZSTD_isError(rc)) {
1313
return 0;
1414
}
15-
return (int)rc;
15+
return (int64_t)rc;
1616
}
1717

1818
int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t dstCap) {
@@ -31,6 +31,6 @@ size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen) {
3131
return (size_t)n;
3232
}
3333

34-
size_t cbm_zstd_compress_bound(int inputSize) {
35-
return ZSTD_compressBound((size_t)inputSize);
34+
size_t cbm_zstd_compress_bound(size_t inputSize) {
35+
return ZSTD_compressBound(inputSize);
3636
}

‎internal/cbm/zstd_store.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include <stdint.h>
66

77
// Zstd compression at specified level (1=fast .. 22=best).
8-
// Returns compressed size on success, 0 on error.
9-
int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int level);
8+
// srcLen/dstCap are size_t for the same reason decompression's are: a >2 GiB
9+
// database would otherwise wrap through int and hand the encoder a capacity that
10+
// disagrees with the real buffer. Returns compressed size on success, 0 on error.
11+
int64_t cbm_zstd_compress(const char *src, size_t srcLen, char *dst, size_t dstCap, int level);
1012

1113
// Zstd decompression. srcLen/dstCap are size_t so a >2 GiB destination capacity
1214
// is never truncated (a large DB artifact, or a crafted one, would otherwise
@@ -20,6 +22,6 @@ int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t ds
2022
size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen);
2123

2224
// Maximum compressed size bound for given input size.
23-
size_t cbm_zstd_compress_bound(int inputSize);
25+
size_t cbm_zstd_compress_bound(size_t inputSize);
2426

2527
#endif // CBM_ZSTD_STORE_H

‎src/discover/discover.c‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "foundation/constants.h"
1515
#include "foundation/compat_fs.h"
16+
#include "foundation/workspace.h"
1617
#include "foundation/platform.h"
1718
#ifdef _WIN32
1819
#include "foundation/win_utf8.h"
@@ -562,6 +563,30 @@ static bool is_safety_core_dir(const char *name) {
562563
}
563564

564565
/* Check if a directory entry should be skipped (hardcoded dirs + gitignore). */
566+
/* The cache directory holds every indexed project's graph database. When a custom
567+
* CBM_CACHE_DIR sits inside a repository — which happens in tests and is legal in
568+
* production — walking into it would pull other projects' databases into this
569+
* project's file list. Prune it by absolute path.
570+
*
571+
* This is the narrow remedy for a concern that was briefly implemented as
572+
* refusing any root containing the cache: refusing a whole root was too blunt,
573+
* and not walking the cache is what the concern actually asks for. */
574+
static bool dir_is_cache_tree(const char *abs_path) {
575+
const char *cache = cbm_workspace_cache_dir();
576+
if (!cache || !cache[0] || !abs_path || !abs_path[0]) {
577+
return false;
578+
}
579+
size_t n = strlen(cache);
580+
while (n > 1 && (cache[n - 1] == '/' || cache[n - 1] == '\\')) {
581+
n--;
582+
}
583+
if (strncmp(abs_path, cache, n) != 0) {
584+
return false;
585+
}
586+
/* Boundary-aware so "<cache>x" is not treated as living under "<cache>". */
587+
return abs_path[n] == '\0' || abs_path[n] == '/' || abs_path[n] == '\\';
588+
}
589+
565590
static bool should_skip_directory(const char *entry_name, const char *rel_path,
566591
const cbm_discover_opts_t *opts, const cbm_gitignore_t *gitignore,
567592
const cbm_gitignore_t *global_gi,
@@ -853,7 +878,8 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
853878
}
854879

855880
if (S_ISDIR(st.st_mode)) {
856-
if (!should_skip_directory(entry->name, rel_path, opts, gitignore, global_gi, cbmignore,
881+
if (!dir_is_cache_tree(abs_path) &&
882+
!should_skip_directory(entry->name, rel_path, opts, gitignore, global_gi, cbmignore,
857883
frame->local_gi, frame->local_gi_prefix)) {
858884
walk_push_subdir(ws, abs_path, rel_path, frame, out);
859885
} else {

‎src/pipeline/artifact.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,14 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
589589
}
590590

591591
/* Compress with zstd */
592-
size_t bound = cbm_zstd_compress_bound((int)db_size);
592+
size_t bound = cbm_zstd_compress_bound(db_size);
593593
char *compressed = malloc(bound);
594594
if (!compressed) {
595595
free(db_data);
596596
return artifact_export_fail("compress", NULL, "alloc_compressed_buffer", 0);
597597
}
598598

599-
int clen = cbm_zstd_compress(db_data, (int)db_size, compressed, (int)bound, compression_level);
599+
int64_t clen = cbm_zstd_compress(db_data, db_size, compressed, bound, compression_level);
600600
free(db_data);
601601

602602
if (clen <= 0) {

‎tests/test_discover.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,45 @@ TEST(pattern_dts_full) {
297297

298298
/* ── File discovery (integration) — cross-platform via test_helpers.h ── */
299299

300+
/* A custom CBM_CACHE_DIR may legitimately sit inside a repository — tests do it
301+
* routinely. Walking into it would pull every other project's graph database into
302+
* this project's file list, so the walk prunes it by absolute path. */
303+
TEST(discover_prunes_the_cache_tree) {
304+
char *base = th_mktempdir("cbm_disc_cache");
305+
ASSERT(base != NULL);
306+
307+
th_write_file(TH_PATH(base, "src/main.go"), "package main\n");
308+
/* Source-looking files inside the cache must not be discovered. */
309+
th_write_file(TH_PATH(base, "cache/other_project/leaked.go"), "package leaked\n");
310+
311+
const char *saved = getenv("CBM_CACHE_DIR");
312+
char *saved_copy = saved ? strdup(saved) : NULL;
313+
char cache_dir[1024];
314+
snprintf(cache_dir, sizeof(cache_dir), "%s/cache", base);
315+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
316+
317+
cbm_discover_opts_t opts = {0};
318+
cbm_file_info_t *files = NULL;
319+
int count = 0;
320+
int rc = cbm_discover(base, &opts, &files, &count);
321+
322+
if (saved_copy) {
323+
cbm_setenv("CBM_CACHE_DIR", saved_copy, 1);
324+
free(saved_copy);
325+
} else {
326+
cbm_unsetenv("CBM_CACHE_DIR");
327+
}
328+
329+
ASSERT_EQ(rc, 0);
330+
for (int i = 0; i < count; i++) {
331+
ASSERT(strstr(files[i].rel_path, "leaked.go") == NULL);
332+
}
333+
ASSERT_EQ(count, 1); /* only src/main.go */
334+
cbm_discover_free(files, count);
335+
th_cleanup(base);
336+
PASS();
337+
}
338+
300339
TEST(discover_simple) {
301340
char *base = th_mktempdir("cbm_disc_simple");
302341
ASSERT(base != NULL);
@@ -1359,6 +1398,7 @@ TEST(discover_many_nested_gitignores_do_not_exhaust_matcher_ownership) {
13591398
/* ── Suite ─────────────────────────────────────────────────────── */
13601399

13611400
SUITE(discover) {
1401+
RUN_TEST(discover_prunes_the_cache_tree);
13621402
/* Directory skip — always */
13631403
RUN_TEST(skip_git);
13641404
RUN_TEST(skip_node_modules);

0 commit comments

Comments
 (0)