osdep/io: fix leak of environment strings on win32

This commit is contained in:
Kacper Michajłow 2024-05-12 18:33:00 +02:00
parent 4ad1fa28b4
commit 9ffbc1eef9
1 changed files with 4 additions and 2 deletions

View File

@ -664,11 +664,12 @@ static void free_env(void)
static void init_getenv(void) static void init_getenv(void)
{ {
#if !HAVE_UWP #if !HAVE_UWP
wchar_t *wenv = GetEnvironmentStringsW(); wchar_t *wenv_begin = GetEnvironmentStringsW();
if (!wenv) if (!wenv_begin)
return; return;
utf8_environ_ctx = talloc_new(NULL); utf8_environ_ctx = talloc_new(NULL);
int num_env = 0; int num_env = 0;
wchar_t *wenv = wenv_begin;
while (1) { while (1) {
size_t len = wcslen(wenv); size_t len = wcslen(wenv);
if (!len) if (!len)
@ -677,6 +678,7 @@ static void init_getenv(void)
MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, s); MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, s);
wenv += len + 1; wenv += len + 1;
} }
FreeEnvironmentStringsW(wenv_begin);
MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, NULL); MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, NULL);
// Avoid showing up in leak detectors etc. // Avoid showing up in leak detectors etc.
atexit(free_env); atexit(free_env);