Win32: use low-level memory allocation during initialization

As of d41489a6 "Add more large blob test cases", git's high-level memory allocation functions (xmalloc, xmemdupz etc.) access the environment to simulate limited memory in tests (see 'getenv("GIT_ALLOC_LIMIT")' in memory_limit_check()). These functions should not be used before the environment is fully initialized (particularly not to initialize the environment itself). The current solution ('environ = NULL; ALLOC_GROW(environ...)') only works because MSVCRT's getenv() reinitializes environ when it is NULL (i.e. it leaves us with two sets of unusabe (non-UTF-8) and unfreeable (CRT- allocated) environments). Add our own set of malloc-or-die functions to be used in startup code. Also check the result of __wgetmainargs, which may fail if there's not enough memory for wide-char arguments and environment. This patch is in preparation of the sorted environment feature, which completely replaces MSVCRT's getenv() implementation. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Stepan Kasal <kasal@ucw.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jul 17, 2014 at 17:38 UTC 6f1c189cadd7c18c03138070324af02563c0b0d1
1 file changed +28 -15
compat/mingw.c
+28 -15
@@ -2033,9 +2033,23 @@ static NORETURN void die_startup()
2033 exit(128);
2034 }
2035
2036 +static void *malloc_startup(size_t size)
2037 +{
2038 + void *result = malloc(size);
2039 + if (!result)
2040 + die_startup();
2041 + return result;
2042 +}
2043 +
2044 +static char *wcstoutfdup_startup(char *buffer, const wchar_t *wcs, size_t len)
2045 +{
2046 + len = xwcstoutf(buffer, wcs, len) + 1;
2047 + return memcpy(malloc_startup(len), buffer, len);
2048 +}
2049 +
2050 void mingw_startup()
2051 {
2038 - int i, len, maxlen, argc;
2052 + int i, maxlen, argc;
2053 char *buffer;
2054 wchar_t **wenv, **wargv;
2055 _startupinfo si;
@@ -2052,26 +2066,25 @@ void mingw_startup()
2066 for (i = 0; wenv[i]; i++)
2067 maxlen = max(maxlen, wcslen(wenv[i]));
2068
2055 - /* nedmalloc can't free CRT memory, allocate resizable environment list */
2056 - environ = NULL;
2069 + /*
2070 + * nedmalloc can't free CRT memory, allocate resizable environment
2071 + * list. Note that xmalloc / xmemdupz etc. call getenv, so we cannot
2072 + * use it while initializing the environment itself.
2073 + */
2074 environ_size = i + 1;
2058 - ALLOC_GROW(environ, environ_size * sizeof(char*), environ_alloc);
2075 + environ_alloc = alloc_nr(environ_size * sizeof(char*));
2076 + environ = malloc_startup(environ_alloc);
2077
2078 /* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
2079 maxlen = 3 * maxlen + 1;
2062 - buffer = xmalloc(maxlen);
2080 + buffer = malloc_startup(maxlen);
2081
2082 /* convert command line arguments and environment to UTF-8 */
2065 - len = xwcstoutf(buffer, _wpgmptr, maxlen);
2066 - __argv[0] = xmemdupz(buffer, len);
2067 - for (i = 1; i < argc; i++) {
2068 - len = xwcstoutf(buffer, wargv[i], maxlen);
2069 - __argv[i] = xmemdupz(buffer, len);
2070 - }
2071 - for (i = 0; wenv[i]; i++) {
2072 - len = xwcstoutf(buffer, wenv[i], maxlen);
2073 - environ[i] = xmemdupz(buffer, len);
2074 - }
2083 + __argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
2084 + for (i = 1; i < argc; i++)
2085 + __argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2086 + for (i = 0; wenv[i]; i++)
2087 + environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2088 environ[i] = NULL;
2089 free(buffer);
2090