Skip to content

Commit 6f1c189

Browse files
kbleesgitster
authored andcommitted
Win32: use low-level memory allocation during initialization
As of d41489a "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 <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f279242 commit 6f1c189

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

compat/mingw.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,9 +2033,23 @@ static NORETURN void die_startup()
20332033
exit(128);
20342034
}
20352035

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+
20362050
void mingw_startup()
20372051
{
2038-
int i, len, maxlen, argc;
2052+
int i, maxlen, argc;
20392053
char *buffer;
20402054
wchar_t **wenv, **wargv;
20412055
_startupinfo si;
@@ -2052,26 +2066,25 @@ void mingw_startup()
20522066
for (i = 0; wenv[i]; i++)
20532067
maxlen = max(maxlen, wcslen(wenv[i]));
20542068

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+
*/
20572074
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);
20592077

20602078
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
20612079
maxlen = 3 * maxlen + 1;
2062-
buffer = xmalloc(maxlen);
2080+
buffer = malloc_startup(maxlen);
20632081

20642082
/* 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);
20752088
environ[i] = NULL;
20762089
free(buffer);
20772090

0 commit comments

Comments
 (0)