Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit d0f63e9

Browse files
kbleeskasal
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]>
1 parent 7f77021 commit d0f63e9

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

compat/mingw.c

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,16 +2001,37 @@ typedef struct {
20012001
extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***env, int glob,
20022002
_startupinfo *si);
20032003

2004+
static NORETURN void die_startup()
2005+
{
2006+
fputs("fatal: not enough memory for initialization", stderr);
2007+
exit(128);
2008+
}
2009+
2010+
static void *malloc_startup(size_t size)
2011+
{
2012+
void *result = malloc(size);
2013+
if (!result)
2014+
die_startup();
2015+
return result;
2016+
}
2017+
2018+
static char *wcstoutfdup_startup(char *buffer, const wchar_t *wcs, size_t len)
2019+
{
2020+
len = xwcstoutf(buffer, wcs, len) + 1;
2021+
return memcpy(malloc_startup(len), buffer, len);
2022+
}
2023+
20042024
void mingw_startup()
20052025
{
2006-
int i, len, maxlen, argc;
2026+
int i, maxlen, argc;
20072027
char *buffer;
20082028
wchar_t **wenv, **wargv;
20092029
_startupinfo si;
20102030

20112031
/* get wide char arguments and environment */
20122032
si.newmode = 0;
2013-
__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si);
2033+
if (__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si) < 0)
2034+
die_startup();
20142035

20152036
/* determine size of argv and environ conversion buffer */
20162037
maxlen = wcslen(_wpgmptr);
@@ -2019,26 +2040,25 @@ void mingw_startup()
20192040
for (i = 0; wenv[i]; i++)
20202041
maxlen = max(maxlen, wcslen(wenv[i]));
20212042

2022-
/* nedmalloc can't free CRT memory, allocate resizable environment list */
2023-
environ = NULL;
2043+
/*
2044+
* nedmalloc can't free CRT memory, allocate resizable environment
2045+
* list. Note that xmalloc / xmemdupz etc. call getenv, so we cannot
2046+
* use it while initializing the environment itself.
2047+
*/
20242048
environ_size = i + 1;
2025-
ALLOC_GROW(environ, environ_size * sizeof(char*), environ_alloc);
2049+
environ_alloc = alloc_nr(environ_size * sizeof(char*));
2050+
environ = malloc_startup(environ_alloc);
20262051

20272052
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
20282053
maxlen = 3 * maxlen + 1;
2029-
buffer = xmalloc(maxlen);
2054+
buffer = malloc_startup(maxlen);
20302055

20312056
/* convert command line arguments and environment to UTF-8 */
2032-
len = xwcstoutf(buffer, _wpgmptr, maxlen);
2033-
__argv[0] = xmemdupz(buffer, len);
2034-
for (i = 1; i < argc; i++) {
2035-
len = xwcstoutf(buffer, wargv[i], maxlen);
2036-
__argv[i] = xmemdupz(buffer, len);
2037-
}
2038-
for (i = 0; wenv[i]; i++) {
2039-
len = xwcstoutf(buffer, wenv[i], maxlen);
2040-
environ[i] = xmemdupz(buffer, len);
2041-
}
2057+
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
2058+
for (i = 1; i < argc; i++)
2059+
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2060+
for (i = 0; wenv[i]; i++)
2061+
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
20422062
environ[i] = NULL;
20432063
free(buffer);
20442064

0 commit comments

Comments
 (0)