Skip to content

Commit e19a1ff

Browse files
committed
msvc: work around a bug in GetEnvironmentVariable()
The return value of that function is 0 both for variables that are unset, as well as for variables whose values are empty. To discern those two cases, one has to call `GetLastError()`, whose return value is `ERROR_ENVVAR_NOT_FOUND` and `ERROR_SUCCESS`, respectively. Except that it is not actually set to `ERROR_SUCCESS` in the latter case, apparently, but the last error value seems to be simply unchanged. To work around this, let's just re-set the last error value just before inspecting the environment variable. This fixes a problem that triggers failures in t3301-notes.sh (where we try to override config settings by passing empty values for certain environment variables). This problem is hidden in the MINGW build by the fact that older MSVC runtimes (such as the one used by MINGW builds) have a `calloc()` that re-sets the last error value in case of success, while newer runtimes set the error value only if `NULL` is returned by that function. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9e78f8b commit e19a1ff

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

compat/mingw.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,8 @@ char *mingw_getenv(const char *name)
16631663
if (!w_key)
16641664
die("Out of memory, (tried to allocate %u wchar_t's)", len_key);
16651665
xutftowcs(w_key, name, len_key);
1666+
/* GetEnvironmentVariableW() only sets the last error upon failure */
1667+
SetLastError(ERROR_SUCCESS);
16661668
len_value = GetEnvironmentVariableW(w_key, w_value, ARRAY_SIZE(w_value));
16671669
if (!len_value && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
16681670
free(w_key);

0 commit comments

Comments
 (0)