Skip to content

Commit c4e37c1

Browse files
authored
Merge pull request #2290 from knewstimek/pr/windows-setenv-utf8
fix(windows): set UTF-8 environment values through wide CRT
2 parents f1efbf6 + 3bd645e commit c4e37c1

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set
3131
#include "foundation/str_util.h" // cbm_json_escape — variants file_path
3232
#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState
33+
#include "foundation/platform.h"
3334
#include "foundation/constants.h"
3435
#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free/mi_usable_size — bind 3rd-party allocators (#424)
3536
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
@@ -1259,7 +1260,9 @@ static int count_params_from_signature(const char *sig) {
12591260
* unlucky in-flight file is never quarantined alone. The env var is set
12601261
* solely by the supervisor during recovery — a no-op on normal runs. */
12611262
static void cbm_index_mark(const char *rel_path, char event) {
1262-
const char *mf = getenv("CBM_INDEX_MARKER_FILE");
1263+
char marker_path[CBM_SZ_4K];
1264+
const char *mf =
1265+
cbm_safe_getenv("CBM_INDEX_MARKER_FILE", marker_path, sizeof(marker_path), NULL);
12631266
if (!mf || !mf[0] || !rel_path || !rel_path[0]) {
12641267
return;
12651268
}
@@ -1296,7 +1299,9 @@ enum { CBM_QSET_UNINIT = 0, CBM_QSET_INITING = 1, CBM_QSET_INITED = 2 };
12961299
static atomic_int g_quarantine_state = CBM_QSET_UNINIT;
12971300

12981301
static void cbm_quarantine_load(void) {
1299-
const char *qf = getenv("CBM_INDEX_QUARANTINE_FILE");
1302+
char quarantine_path[CBM_SZ_4K];
1303+
const char *qf = cbm_safe_getenv("CBM_INDEX_QUARANTINE_FILE", quarantine_path,
1304+
sizeof(quarantine_path), NULL);
13001305
if (!qf || !qf[0]) {
13011306
return; /* normal path: empty set */
13021307
}

‎src/cli/cli.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,9 @@ static bool cli_activation_production_context_init(cli_activation_production_con
810810
context->cleanup_ok = true;
811811
context->deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_DRAIN_TIMEOUT_MS);
812812
context->control_deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_CONTROL_TIMEOUT_MS);
813-
const char *original_cache_environment = getenv("CBM_CACHE_DIR");
813+
char original_cache_buffer[CLI_BUF_4K];
814+
const char *original_cache_environment = cbm_safe_getenv("CBM_CACHE_DIR", original_cache_buffer,
815+
sizeof(original_cache_buffer), NULL);
814816
context->original_cache_environment_present = original_cache_environment != NULL;
815817
if (context->original_cache_environment_present) {
816818
context->original_cache_environment = strdup(original_cache_environment);

‎src/foundation/compat.h‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,9 @@ static inline int cbm_setenv(const char *name, const char *value, int overwrite)
166166
free(wide_value);
167167
return EINVAL;
168168
}
169-
/* Keep the CRT's narrow environment useful for legacy getenv callers,
170-
* then repair the process-wide Windows environment with the actual UTF-16
171-
* value. _putenv_s alone routes UTF-8 path bytes through the active ANSI
172-
* code page, which corrupts non-ASCII cache roots inherited by children. */
173-
int status = _putenv_s(name, value);
169+
/* _putenv_s can reject UTF-8 bytes outside the active ANSI code page.
170+
* Narrow getenv returns bytes in that code page after this wide update. */
171+
int status = _wputenv_s(wide_name, wide_value);
174172
if (status == 0 && !SetEnvironmentVariableW(wide_name, wide_value)) {
175173
status = EINVAL;
176174
}

‎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) */
@@ -13339,7 +13340,42 @@ TEST(python_namespace_facts_multiple_bindings_and_unsupported_effects) {
1333913340
PASS();
1334013341
}
1334113342

13343+
#ifdef _WIN32
13344+
/* The supervisor sets this path through cbm_setenv. Exercise the actual
13345+
* journal writer, which must read it as UTF-8 before calling cbm_fopen. */
13346+
TEST(extraction_marker_round_trips_non_ascii_environment_path) {
13347+
char dir[512] = "/tmp/cbm-marker-XXXXXX";
13348+
ASSERT_NOT_NULL(cbm_mkdtemp(dir));
13349+
char marker[640];
13350+
snprintf(marker, sizeof(marker), "%s/marker-\xce\x94-\xe4\xb8\x81.log", dir);
13351+
char saved[4096];
13352+
const char *previous = cbm_safe_getenv("CBM_INDEX_MARKER_FILE", saved, sizeof(saved), NULL);
13353+
ASSERT_EQ(cbm_setenv("CBM_INDEX_MARKER_FILE", marker, 1), 0);
13354+
cbm_index_mark_start("synthetic.cpp");
13355+
cbm_index_mark_done("synthetic.cpp");
13356+
if (previous) {
13357+
(void)cbm_setenv("CBM_INDEX_MARKER_FILE", saved, 1);
13358+
} else {
13359+
(void)cbm_unsetenv("CBM_INDEX_MARKER_FILE");
13360+
}
13361+
FILE *file = cbm_fopen(marker, "rb");
13362+
char contents[128] = {0};
13363+
if (file) {
13364+
(void)fread(contents, 1, sizeof(contents) - 1, file);
13365+
(void)fclose(file);
13366+
}
13367+
(void)cbm_unlink(marker);
13368+
(void)cbm_rmdir(dir);
13369+
ASSERT_NOT_NULL(file);
13370+
ASSERT_STR_EQ(contents, "S synthetic.cpp\nD synthetic.cpp\n");
13371+
PASS();
13372+
}
13373+
#endif
13374+
1334213375
SUITE(extraction) {
13376+
#ifdef _WIN32
13377+
RUN_TEST(extraction_marker_round_trips_non_ascii_environment_path);
13378+
#endif
1334313379
RUN_TEST(extract_compact_keeps_every_field_and_shrinks_the_arena);
1334413380
RUN_TEST(extract_compact_is_idempotent_and_survives_empty_results);
1334513381
RUN_TEST(extract_spill_round_trip_keeps_every_field);

‎tests/test_platform.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,15 +947,23 @@ TEST(platform_env_dup_honours_the_settings_resolver) {
947947
* setter must update that same wide environment; _putenv_s alone interprets
948948
* UTF-8 path bytes through the active ANSI code page. */
949949
TEST(platform_setenv_preserves_utf8_in_wide_environment) {
950-
static const char utf8[] = "C:/cbm-cache-\xce\x94-\xe6\x97\xa5\xe6\x9c\xac";
951-
static const wchar_t wide[] = L"C:/cbm-cache-\u0394-\u65e5\u672c";
950+
static const char utf8[] = "C:/cbm-cache-\xce\x94-\xe6\x97\xa5\xe6\x9c\xac-\xe4\xb8\x81";
951+
static const wchar_t wide[] = L"C:/cbm-cache-\u0394-\u65e5\u672c-\u4e01";
952952
ASSERT_EQ(cbm_setenv("CBM_CACHE_DIR", utf8, 1), 0);
953953
wchar_t observed_wide[128];
954954
ASSERT_EQ(GetEnvironmentVariableW(L"CBM_CACHE_DIR", observed_wide, 128), (DWORD)(wcslen(wide)));
955955
ASSERT_EQ(wcscmp(observed_wide, wide), 0);
956956
char observed_utf8[128];
957957
ASSERT_NOT_NULL(cbm_safe_getenv("CBM_CACHE_DIR", observed_utf8, sizeof(observed_utf8), NULL));
958958
ASSERT_STR_EQ(observed_utf8, utf8);
959+
/* Narrow getenv must agree with the CRT's active ANSI code page. */
960+
char expected_narrow[128];
961+
ASSERT_GT(WideCharToMultiByte(CP_ACP, 0, wide, -1, expected_narrow,
962+
(int)sizeof(expected_narrow), NULL, NULL),
963+
0);
964+
const char *observed_narrow = getenv("CBM_CACHE_DIR");
965+
ASSERT_NOT_NULL(observed_narrow);
966+
ASSERT_STR_EQ(observed_narrow, expected_narrow);
959967
(void)cbm_unsetenv("CBM_CACHE_DIR");
960968
PASS();
961969
}

0 commit comments

Comments
 (0)