Skip to content

Commit 26e2d0c

Browse files
committed
fix(windows): read worker and CLI environment paths as UTF-8
Signed-off-by: News <knewstimek@users.noreply.github.com>
1 parent 2554631 commit 26e2d0c

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "foundation/compat_fs.h" // cbm_fopen — crash-supervisor per-file marker write
2626
#include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set
2727
#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState
28+
#include "foundation/platform.h"
2829
#include "foundation/constants.h"
2930
#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free/mi_usable_size — bind 3rd-party allocators (#424)
3031
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
@@ -969,7 +970,9 @@ static int count_params_from_signature(const char *sig) {
969970
* unlucky in-flight file is never quarantined alone. The env var is set
970971
* solely by the supervisor during recovery — a no-op on normal runs. */
971972
static void cbm_index_mark(const char *rel_path, char event) {
972-
const char *mf = getenv("CBM_INDEX_MARKER_FILE");
973+
char marker_path[CBM_SZ_4K];
974+
const char *mf =
975+
cbm_safe_getenv("CBM_INDEX_MARKER_FILE", marker_path, sizeof(marker_path), NULL);
973976
if (!mf || !mf[0] || !rel_path || !rel_path[0]) {
974977
return;
975978
}
@@ -1006,7 +1009,9 @@ enum { CBM_QSET_UNINIT = 0, CBM_QSET_INITING = 1, CBM_QSET_INITED = 2 };
10061009
static atomic_int g_quarantine_state = CBM_QSET_UNINIT;
10071010

10081011
static void cbm_quarantine_load(void) {
1009-
const char *qf = getenv("CBM_INDEX_QUARANTINE_FILE");
1012+
char quarantine_path[CBM_SZ_4K];
1013+
const char *qf = cbm_safe_getenv("CBM_INDEX_QUARANTINE_FILE", quarantine_path,
1014+
sizeof(quarantine_path), NULL);
10101015
if (!qf || !qf[0]) {
10111016
return; /* normal path: empty set */
10121017
}

‎src/cli/cli.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,9 @@ static bool cli_activation_production_context_init(cli_activation_production_con
759759
context->cleanup_ok = true;
760760
context->deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_DRAIN_TIMEOUT_MS);
761761
context->control_deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_CONTROL_TIMEOUT_MS);
762-
const char *original_cache_environment = getenv("CBM_CACHE_DIR");
762+
char original_cache_buffer[CLI_BUF_4K];
763+
const char *original_cache_environment = cbm_safe_getenv("CBM_CACHE_DIR", original_cache_buffer,
764+
sizeof(original_cache_buffer), NULL);
763765
context->original_cache_environment_present = original_cache_environment != NULL;
764766
if (context->original_cache_environment_present) {
765767
context->original_cache_environment = strdup(original_cache_environment);

‎tests/test_extraction.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include "test_framework.h"
99
#include "cbm.h"
10+
#include "foundation/platform.h"
1011
#include "foundation/constants.h" /* CBM_SZ_* */
1112
#include "preprocessor.h" /* cbm_export_macro_candidates (#1989) */
1213
#include "../src/foundation/compat.h" /* cbm_clock_gettime (wide-flat scaling guard) */
@@ -8157,7 +8158,42 @@ TEST(extract_walk_truncated_when_a_node_budget_is_set) {
81578158
PASS();
81588159
}
81598160

8161+
#ifdef _WIN32
8162+
/* The supervisor sets this path through cbm_setenv. Exercise the actual
8163+
* journal writer, which must read it as UTF-8 before calling cbm_fopen. */
8164+
TEST(extraction_marker_round_trips_non_ascii_environment_path) {
8165+
char dir[512] = "/tmp/cbm-marker-XXXXXX";
8166+
ASSERT_NOT_NULL(cbm_mkdtemp(dir));
8167+
char marker[640];
8168+
snprintf(marker, sizeof(marker), "%s/marker-\xce\x94-\xe4\xb8\x81.log", dir);
8169+
char saved[4096];
8170+
const char *previous = cbm_safe_getenv("CBM_INDEX_MARKER_FILE", saved, sizeof(saved), NULL);
8171+
ASSERT_EQ(cbm_setenv("CBM_INDEX_MARKER_FILE", marker, 1), 0);
8172+
cbm_index_mark_start("synthetic.cpp");
8173+
cbm_index_mark_done("synthetic.cpp");
8174+
if (previous) {
8175+
(void)cbm_setenv("CBM_INDEX_MARKER_FILE", saved, 1);
8176+
} else {
8177+
(void)cbm_unsetenv("CBM_INDEX_MARKER_FILE");
8178+
}
8179+
FILE *file = cbm_fopen(marker, "rb");
8180+
char contents[128] = {0};
8181+
if (file) {
8182+
(void)fread(contents, 1, sizeof(contents) - 1, file);
8183+
(void)fclose(file);
8184+
}
8185+
(void)cbm_unlink(marker);
8186+
(void)cbm_rmdir(dir);
8187+
ASSERT_NOT_NULL(file);
8188+
ASSERT_STR_EQ(contents, "S synthetic.cpp\nD synthetic.cpp\n");
8189+
PASS();
8190+
}
8191+
#endif
8192+
81608193
SUITE(extraction) {
8194+
#ifdef _WIN32
8195+
RUN_TEST(extraction_marker_round_trips_non_ascii_environment_path);
8196+
#endif
81618197
RUN_TEST(extract_compact_keeps_every_field_and_shrinks_the_arena);
81628198
RUN_TEST(extract_compact_is_idempotent_and_survives_empty_results);
81638199
RUN_TEST(extract_spill_round_trip_keeps_every_field);

0 commit comments

Comments
 (0)