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

Function cbm_subprocess_copy_environment

src/foundation/subprocess.c:488–566  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

486}
487
488static bool cbm_subprocess_copy_environment(cbm_subprocess_t *process, const char *const *overrides,
489 size_t override_count) {
490 wchar_t *inherited =
491 process->strip_git_repo_env ? cbm_git_child_env_block() : GetEnvironmentStringsW();
492 if (!inherited) {
493 return false;
494 }
495 size_t count = 0;
496 for (const wchar_t *entry = inherited; *entry; entry += wcslen(entry) + 1) {
497 count++;
498 }
499 wchar_t **entries = NULL;
500 wchar_t **converted = NULL;
501 bool ok = false;
502 if (override_count > SIZE_MAX / sizeof(*entries) ||
503 count > SIZE_MAX / sizeof(*entries) - override_count) {
504 goto done;
505 }
506 entries = cbm_calloc(CBM_MEM_CLASS_OTHER, (count + override_count) * sizeof(*entries));
507 converted = cbm_calloc(CBM_MEM_CLASS_OTHER, override_count * sizeof(*converted));
508 if (!entries || !converted) {
509 goto done;
510 }
511 size_t index = 0;
512 for (wchar_t *entry = inherited; *entry; entry += wcslen(entry) + 1) {
513 entries[index++] = entry;
514 }
515 for (size_t i = 0; i < override_count; i++) {
516 if (process->strip_git_repo_env && cbm_git_env_entry_should_strip(overrides[i])) {
517 continue;
518 }
519 converted[i] = cbm_utf8_to_wide_in(CBM_MEM_CLASS_OTHER, overrides[i]);
520 if (!converted[i]) {
521 goto done;
522 }
523 size_t slot = 0;
524 while (slot < count && cbm_win_environment_compare(&entries[slot], &converted[i]) != 0) {
525 slot++;
526 }
527 entries[slot] = converted[i];
528 if (slot == count) {
529 count++;
530 }
531 }
532 qsort(entries, count, sizeof(*entries), cbm_win_environment_compare);
533 size_t length = count ? 1 : 2; /* Empty blocks also need two NUL characters. */
534 for (size_t i = 0; i < count; i++) {
535 size_t entry_length = wcslen(entries[i]) + 1;
536 if (entry_length > SIZE_MAX / sizeof(wchar_t) - length) {
537 goto done;
538 }
539 length += entry_length;
540 }
541 process->environment = cbm_calloc(CBM_MEM_CLASS_OTHER, length * sizeof(*process->environment));
542 if (!process->environment) {
543 goto done;
544 }
545 wchar_t *next = process->environment;

Callers 1

cbm_subprocess_copy_optsFunction · 0.85

Calls 8

cbm_git_child_env_blockFunction · 0.85
cbm_callocFunction · 0.85
cbm_utf8_to_wide_inFunction · 0.85
cbm_freeFunction · 0.85
cbm_git_child_env_freeFunction · 0.85
cbm_mem_strdupFunction · 0.85

Tested by

no test coverage detected