@@ -14985,6 +14985,260 @@ TEST(cli_hook_augment_deadline_breadcrumb_issue858) {
1498514985#endif
1498614986}
1498714987
14988+ /* ── hook-augment breadcrumb log location ──────────────────────────
14989+ * Arming the deadline opens the breadcrumb log with O_CREAT. Its path was
14990+ * "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log" built in
14991+ * 1 KiB buffers with no length check, so a long HOME created a file at a
14992+ * cut-off name, and CBM_CACHE_DIR, which every other cbm log honours, was
14993+ * ignored. These run the real entry point in a child with a no-op Bash
14994+ * payload: the deadline is armed (the log opened), then the hook passes
14995+ * through with exit 0. POSIX only: the in-process deadline and its log are
14996+ * compiled out on Windows. */
14997+ #ifndef _WIN32
14998+ enum { HL_PATH_CAP = 1024, HL_SEGMENT = 200 };
14999+
15000+ /* Exit code of `hook-augment` run in a child with HOME = home and
15001+ * CBM_CACHE_DIR = cache_dir (unset when NULL), or -1 when it did not exit. */
15002+ static int hl_run_hook(const char *home, const char *cache_dir) {
15003+ static const char payload[] = "{\"hook_event_name\":\"PreToolUse\",\"tool_name\":\"Bash\","
15004+ "\"tool_input\":{\"command\":\"ls\"}}";
15005+ int fds[2];
15006+ if (pipe(fds) != 0) {
15007+ return -1;
15008+ }
15009+ /* Queued before the fork, so no write can meet a reader that has gone. */
15010+ ssize_t queued = write(fds[1], payload, sizeof(payload) - 1);
15011+ close(fds[1]);
15012+ if (queued != (ssize_t)(sizeof(payload) - 1)) {
15013+ close(fds[0]);
15014+ return -1;
15015+ }
15016+ fflush(NULL);
15017+ pid_t pid = fork();
15018+ if (pid == 0) {
15019+ dup2(fds[0], STDIN_FILENO);
15020+ close(fds[0]);
15021+ cbm_setenv("HOME", home, 1);
15022+ cbm_unsetenv("USERPROFILE");
15023+ cbm_unsetenv("CBM_HOOK_TIMEOUT_LOG");
15024+ if (cache_dir) {
15025+ cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
15026+ } else {
15027+ cbm_unsetenv("CBM_CACHE_DIR");
15028+ }
15029+ _exit(cbm_cmd_hook_augment(0, NULL));
15030+ }
15031+ close(fds[0]);
15032+ int status = 0;
15033+ if (pid < 0 || waitpid(pid, &status, 0) != pid || !WIFEXITED(status)) {
15034+ return -1;
15035+ }
15036+ return WEXITSTATUS(status);
15037+ }
15038+
15039+ /* A fresh synthetic root with its links resolved. The kernel bounds the
15040+ * EXPANDED path, and macOS /tmp is a link to /private/tmp: under the link a
15041+ * near-1 KiB path can be neither created nor probed, which hides the very
15042+ * cut-off file these tests look for. */
15043+ static bool hl_make_root(char *root, size_t root_sz) {
15044+ char tmpl[256];
15045+ char real[4096];
15046+ snprintf(tmpl, sizeof(tmpl), "/tmp/cbm-hooklog-XXXXXX");
15047+ if (!cbm_mkdtemp(tmpl)) {
15048+ return false;
15049+ }
15050+ if (!cbm_canonical_path(tmpl, real, sizeof(real))) {
15051+ th_rmtree(tmpl);
15052+ return false;
15053+ }
15054+ int n = snprintf(root, root_sz, "%s", real);
15055+ return n > 0 && (size_t)n < root_sz;
15056+ }
15057+
15058+ /* "<root>/hhh…/hhh…" of exactly `len` bytes, every component short of
15059+ * NAME_MAX. False when it cannot be built. */
15060+ static bool hl_long_dir(char *out, size_t out_sz, const char *root, size_t len) {
15061+ int n = snprintf(out, out_sz, "%s", root);
15062+ if (n <= 0 || (size_t)n >= len || len >= out_sz) {
15063+ return false;
15064+ }
15065+ size_t at = (size_t)n;
15066+ while (at < len) {
15067+ out[at++] = '/';
15068+ for (int seg = 0; seg < HL_SEGMENT && at < len; seg++) {
15069+ out[at++] = 'h';
15070+ }
15071+ }
15072+ out[at] = '\0';
15073+ return out[len - 1] != '/';
15074+ }
15075+
15076+ /* The first HL_PATH_CAP - 1 bytes of "<base>/<suffix>": the name the old
15077+ * unchecked 1 KiB buffer cut that path down to. */
15078+ static void hl_cut_name(char *out, const char *base, const char *suffix) {
15079+ char full[2 * HL_PATH_CAP];
15080+ snprintf(full, sizeof(full), "%s/%s", base, suffix);
15081+ snprintf(out, HL_PATH_CAP, "%.*s", HL_PATH_CAP - 1, full);
15082+ }
15083+
15084+ static bool hl_is_ancestor_or_self(const char *path, const char *of) {
15085+ size_t n = strlen(path);
15086+ return of && strncmp(of, path, n) == 0 && (of[n] == '/' || of[n] == '\0');
15087+ }
15088+
15089+ /* Entries under dir that are neither keep_a, keep_b nor one of their
15090+ * ancestors: whatever the hook created. Each is printed by its tail. */
15091+ static int hl_count_strays(const char *dir, const char *keep_a, const char *keep_b) {
15092+ cbm_dir_t *d = cbm_opendir(dir);
15093+ if (!d) {
15094+ return 0;
15095+ }
15096+ int strays = 0;
15097+ cbm_dirent_t *entry;
15098+ while ((entry = cbm_readdir(d)) != NULL) {
15099+ if (strcmp(entry->name, ".") == 0 || strcmp(entry->name, "..") == 0) {
15100+ continue;
15101+ }
15102+ char child[HL_PATH_CAP];
15103+ int n = snprintf(child, sizeof(child), "%s/%s", dir, entry->name);
15104+ bool named = n > 0 && (size_t)n < sizeof(child);
15105+ if (named &&
15106+ (hl_is_ancestor_or_self(child, keep_a) || hl_is_ancestor_or_self(child, keep_b))) {
15107+ if (entry->is_dir) {
15108+ strays += hl_count_strays(child, keep_a, keep_b);
15109+ }
15110+ continue;
15111+ }
15112+ size_t len = named ? (size_t)n : strlen(child);
15113+ printf(" stray entry: ...%s\n", child + (len > 60 ? len - 60 : 0));
15114+ strays++;
15115+ }
15116+ cbm_closedir(d);
15117+ return strays;
15118+ }
15119+ #endif
15120+
15121+ /* A HOME of ~1 KiB: the log path "<HOME>/.cache/codebase-memory-mcp/logs/
15122+ * hook-augment-timeouts.log" does not fit the buffer. The old code opened its
15123+ * cut-off prefix ".../logs/hook-augme" with O_CREAT. Nothing may be created at
15124+ * a cut path (nothing at all under the synthetic root), and the hook must
15125+ * still exit 0: it never fails the host agent's hook call. */
15126+ TEST(cli_hook_augment_log_long_home_creates_no_cut_path) {
15127+ #ifdef _WIN32
15128+ SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15129+ #else
15130+ enum { HL_HOME_LEN = 980 }; /* "<HOME>/.cache/.../logs" fits 1 KiB, the file name does not */
15131+ char root[256];
15132+ if (!hl_make_root(root, sizeof(root))) {
15133+ FAIL("synthetic root failed");
15134+ }
15135+ char home[HL_PATH_CAP];
15136+ bool built = hl_long_dir(home, sizeof(home), root, HL_HOME_LEN) && th_mkdir_p(home) == 0;
15137+ char cut[HL_PATH_CAP];
15138+ hl_cut_name(cut, home, ".cache/codebase-memory-mcp/logs/hook-augment-timeouts.log");
15139+
15140+ int rc = built ? hl_run_hook(home, NULL) : -1;
15141+ bool cut_exists = cbm_file_exists(cut);
15142+ int strays = hl_count_strays(root, home, NULL);
15143+ test_rmdir_r(root);
15144+
15145+ ASSERT_TRUE(built);
15146+ ASSERT_EQ(rc, 0);
15147+ if (cut_exists) {
15148+ printf(" log opened at the cut-off name ...%s\n", cut + strlen(cut) - 40);
15149+ }
15150+ ASSERT_FALSE(cut_exists);
15151+ ASSERT_EQ(strays, 0);
15152+ PASS();
15153+ #endif
15154+ }
15155+
15156+ /* CBM_CACHE_DIR is where every cbm log goes (<cache_dir>/logs), so the hook's
15157+ * breadcrumb goes there too, and not under HOME. A CBM_CACHE_DIR of ~1 KiB
15158+ * whose log path does not fit is no log at all: no cut-off file, no stray
15159+ * directory, and the hook still exits 0. */
15160+ TEST(cli_hook_augment_log_honours_cache_dir) {
15161+ #ifdef _WIN32
15162+ SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15163+ #else
15164+ enum { HL_CACHE_LEN = 1000 }; /* "<cache>/logs" fits 1 KiB, the file name does not */
15165+ char root[256];
15166+ if (!hl_make_root(root, sizeof(root))) {
15167+ FAIL("synthetic root failed");
15168+ }
15169+ char home[512];
15170+ char cache[512];
15171+ char expected[640];
15172+ snprintf(home, sizeof(home), "%s/home", root);
15173+ snprintf(cache, sizeof(cache), "%s/cache", root);
15174+ snprintf(expected, sizeof(expected), "%s/logs/hook-augment-timeouts.log", cache);
15175+ bool made = th_mkdir_p(home) == 0 && th_mkdir_p(cache) == 0;
15176+
15177+ /* A configured cache dir holds the log; HOME is left alone. */
15178+ int rc = made ? hl_run_hook(home, cache) : -1;
15179+ bool in_cache = cbm_file_exists(expected);
15180+ int strays = hl_count_strays(root, home, expected);
15181+
15182+ /* A configured cache dir too long for the log path: no log anywhere. */
15183+ char long_cache[HL_PATH_CAP];
15184+ char cut[HL_PATH_CAP];
15185+ bool long_built = hl_long_dir(long_cache, sizeof(long_cache), root, HL_CACHE_LEN) &&
15186+ th_mkdir_p(long_cache) == 0;
15187+ hl_cut_name(cut, long_cache, "logs/hook-augment-timeouts.log");
15188+ bool reset = th_rmtree(home) == 0 && th_rmtree(cache) == 0 && th_mkdir_p(home) == 0;
15189+ int long_rc = long_built && reset ? hl_run_hook(home, long_cache) : -1;
15190+ bool cut_exists = cbm_file_exists(cut);
15191+ int long_strays = hl_count_strays(root, home, long_cache);
15192+ test_rmdir_r(root);
15193+
15194+ ASSERT_TRUE(made);
15195+ ASSERT_EQ(rc, 0);
15196+ if (!in_cache) {
15197+ printf(" no breadcrumb log under CBM_CACHE_DIR/logs\n");
15198+ }
15199+ ASSERT_TRUE(in_cache);
15200+ ASSERT_EQ(strays, 0);
15201+ ASSERT_TRUE(long_built);
15202+ ASSERT_TRUE(reset);
15203+ ASSERT_EQ(long_rc, 0);
15204+ ASSERT_FALSE(cut_exists);
15205+ ASSERT_EQ(long_strays, 0);
15206+ PASS();
15207+ #endif
15208+ }
15209+
15210+ /* Positive control: with a normal HOME and no CBM_CACHE_DIR the breadcrumb log
15211+ * stays exactly where it always was,
15212+ * "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", and the
15213+ * hook creates nothing else. */
15214+ TEST(cli_hook_augment_log_default_location_unchanged) {
15215+ #ifdef _WIN32
15216+ SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
15217+ #else
15218+ char root[256];
15219+ if (!hl_make_root(root, sizeof(root))) {
15220+ FAIL("synthetic root failed");
15221+ }
15222+ char home[512];
15223+ char expected[640];
15224+ snprintf(home, sizeof(home), "%s/home", root);
15225+ snprintf(expected, sizeof(expected),
15226+ "%s/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", home);
15227+ bool made = th_mkdir_p(home) == 0;
15228+
15229+ int rc = made ? hl_run_hook(home, NULL) : -1;
15230+ bool at_default = cbm_file_exists(expected);
15231+ int strays = hl_count_strays(root, expected, NULL);
15232+ test_rmdir_r(root);
15233+
15234+ ASSERT_TRUE(made);
15235+ ASSERT_EQ(rc, 0);
15236+ ASSERT_TRUE(at_default);
15237+ ASSERT_EQ(strays, 0);
15238+ PASS();
15239+ #endif
15240+ }
15241+
1498815242TEST(cli_upsert_claude_hook_existing) {
1498915243 char tmpdir[256];
1499015244 snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-XXXXXX");
@@ -17094,6 +17348,9 @@ SUITE(cli) {
1709417348 RUN_TEST(cli_claude_exec_hooks_custom_dir_uninstall_issue1733);
1709517349 RUN_TEST(cli_hook_augment_path_is_abs);
1709617350 RUN_TEST(cli_hook_augment_deadline_breadcrumb_issue858);
17351+ RUN_TEST(cli_hook_augment_log_long_home_creates_no_cut_path);
17352+ RUN_TEST(cli_hook_augment_log_honours_cache_dir);
17353+ RUN_TEST(cli_hook_augment_log_default_location_unchanged);
1709717354 RUN_TEST(cli_upsert_claude_hook_fresh);
1709817355 RUN_TEST(cli_upsert_claude_hook_existing);
1709917356 RUN_TEST(cli_tool_hooks_preserve_foreign_same_matcher);
0 commit comments