Use GetEnvironmentVariableW instead of _wgetenv to silence VC warnings.

This commit is contained in:
Martin v. Löwis 2012-06-21 16:33:09 +02:00
parent 56bf6f8202
commit f36d65c7c8
1 changed files with 18 additions and 9 deletions

View File

@ -54,22 +54,31 @@ skip_whitespace(wchar_t * p)
} }
/* /*
* This function is here to minimise Visual Studio * This function is here to simplify memory management
* warnings about security implications of getenv, and to * and to treat blank values as if they are absent.
* treat blank values as if they are absent.
*/ */
static wchar_t * get_env(wchar_t * key) static wchar_t * get_env(wchar_t * key)
{ {
wchar_t * result = _wgetenv(key); /* This is not thread-safe, just like getenv */
static wchar_t buf[256];
DWORD result = GetEnvironmentVariableW(key, buf, 256);
if (result) { if (result > 256) {
result = skip_whitespace(result); /* Large environment variable. Accept some leakage */
if (*result == L'\0') wchar_t *buf2 = (wchar_t*)malloc(sizeof(wchar_t) * (result+1));
result = NULL; GetEnvironmentVariableW(key, buf2, result);
return buf2;
} }
return result;
if (result == 0)
/* Either some error, e.g. ERROR_ENVVAR_NOT_FOUND,
or an empty environment variable. */
return NULL;
return buf;
} }
static void static void
debug(wchar_t * format, ...) debug(wchar_t * format, ...)
{ {