diff options
author | Ivan Zhakov <ivan@apache.org> | 2022-06-19 11:06:47 +0000 |
---|---|---|
committer | Ivan Zhakov <ivan@apache.org> | 2022-06-19 11:06:47 +0000 |
commit | 1f1386f286001ed636f0cafcaf37427eca2072ec (patch) | |
tree | 9c058afa41ee021a8edc7b8404605a3da9d8526f /misc | |
parent | ff82341af91856f024b6cb110c73ec379a3b3a3a (diff) | |
download | apr-1f1386f286001ed636f0cafcaf37427eca2072ec.tar.gz |
win32: Fix attempt to free invalid memory on exit when apr_app is used.
* CHANGES: Add changelog entry.
* misc/win32/apr_app.c
(wmain): Allocate separate heap entry for every environment variable.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1902058 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'misc')
-rw-r--r-- | misc/win32/apr_app.c | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/misc/win32/apr_app.c b/misc/win32/apr_app.c index 4e08e33d7..f1b6ce57c 100644 --- a/misc/win32/apr_app.c +++ b/misc/win32/apr_app.c @@ -51,15 +51,37 @@ int wmain(int argc, const wchar_t **wargv, const wchar_t **wenv) { char **argv; char **env; - int dupenv; + int envc; + int i; (void)apr_wastrtoastr(&argv, wargv, argc); - dupenv = apr_wastrtoastr(&env, wenv, -1); + envc = 0; + while (wenv[envc]) { + envc++; + } + + /* Initial environment stored as single heap block, but uses + * separate heap entry for every environment variable + * after first change. + */ + env = apr_malloc_dbg((envc + 1) * sizeof(char *), __FILE__, __LINE__); + + for (i = 0; i < envc; i++) { + apr_size_t wcount; + apr_size_t envlen; + + wcount = wcslen(wenv[i]) + 1; + envlen = (wcount - 1) * 3 + 1; + + env[i] = apr_malloc_dbg(envlen, __FILE__, __LINE__); + + (void)apr_conv_ucs2_to_utf8(wenv[i], &wcount, env[i], &envlen); + } + + env[i] = NULL; - _environ = apr_malloc_dbg((dupenv + 1) * sizeof (char *), - __FILE__, __LINE__ ); - memcpy(_environ, env, (dupenv + 1) * sizeof (char *)); + _environ = env; /* MSVCRT will attempt to maintain the wide environment calls * on _putenv(), which is bogus if we've passed a non-ascii |