Skip to content

Commit aa8ee54

Browse files
jeffhostetlerdscho
authored andcommitted
msvc: convert environment from/to UTF-16 on the fly
This adds MSVC versions of getenv() and friends. These take UTF-8 arguments and return UTF-8 values, but use the UNICODE versions of the CRT routines. This avoids the need to write to __environ (which is only visible if you statically link to the CRT). This also avoids the CP_ACP conversions performed inside the CRT. It also avoids various memory leaks and problems. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 791a07f commit aa8ee54

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed

compat/mingw.c

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,147 @@ static char *path_lookup(const char *cmd, int exe_only)
11741174
return prog;
11751175
}
11761176

1177+
#if defined(_MSC_VER)
1178+
1179+
/* We need a stable sort */
1180+
#ifndef INTERNAL_QSORT
1181+
#include "qsort.c"
1182+
#endif
1183+
1184+
/* Compare only keys */
1185+
static int wenvcmp(const void *a, const void *b)
1186+
{
1187+
wchar_t *p = *(wchar_t **)a, *q = *(wchar_t **)b;
1188+
size_t p_len, q_len;
1189+
int ret;
1190+
1191+
/* Find end of keys */
1192+
for (p_len = 0; p[p_len] && p[p_len] != L'='; p_len++)
1193+
; /* do nothing */
1194+
for (q_len = 0; q[q_len] && q[q_len] != L'='; q_len++)
1195+
; /* do nothing */
1196+
1197+
/* Are keys identical (modulo case)? */
1198+
if (p_len == q_len && !_wcsnicmp(p, q, p_len))
1199+
return 0;
1200+
1201+
ret = _wcsnicmp(p, q, p_len < q_len ? p_len : q_len);
1202+
return ret ? ret : (p_len < q_len ? -1 : +1);
1203+
}
1204+
1205+
/*
1206+
* Build an environment block combining the inherited environment
1207+
* merged with the given list of settings.
1208+
*
1209+
* Values of the form "KEY=VALUE" in deltaenv override inherited values.
1210+
* Values of the form "KEY" in deltaenv delete inherited values.
1211+
*
1212+
* Multiple entries in deltaenv for the same key are explicitly allowed.
1213+
*
1214+
* We return a contiguous block of UNICODE strings with a final trailing
1215+
* zero word.
1216+
*/
1217+
static wchar_t *make_environment_block(char **deltaenv)
1218+
{
1219+
/*
1220+
* The CRT (at least as of UCRT) secretly declares "_wenviron"
1221+
* as a function that returns a pointer to a mostly static table.
1222+
* Grab the pointer and cache it for the duration of our loop.
1223+
*/
1224+
const wchar_t *wenv = GetEnvironmentStringsW(), *p;
1225+
size_t delta_size = 0, size = 1; /* for extra NUL at the end */
1226+
1227+
wchar_t **array = NULL;
1228+
size_t alloc = 0, nr = 0, i;
1229+
1230+
const char *p2;
1231+
wchar_t *wdeltaenv;
1232+
1233+
wchar_t *result, *p3;
1234+
1235+
/*
1236+
* If there is no deltaenv to apply, simply return a copy
1237+
*/
1238+
if (!deltaenv || !*deltaenv) {
1239+
for (p = wenv; p && *p; ) {
1240+
size_t s = wcslen(p) + 1;
1241+
size += s;
1242+
p += s;
1243+
}
1244+
1245+
ALLOC_ARRAY(result, size);
1246+
memcpy(result, wenv, size * sizeof(*wenv));
1247+
FreeEnvironmentStringsW(wenv);
1248+
return result;
1249+
}
1250+
1251+
/*
1252+
* If there is a deltaenv, let's accumulate all keys into `array`,
1253+
* sort them using the stable git_qsort() and then copy, skipping
1254+
* duplicate keys
1255+
*/
1256+
1257+
for (p = wenv; p && *p; ) {
1258+
size_t s = wcslen(p) + 1;
1259+
size += s;
1260+
ALLOC_GROW(array, nr + 1, alloc);
1261+
array[nr++] = p;
1262+
p += s;
1263+
}
1264+
1265+
/* (over-)assess size needed for wchar version of deltaenv */
1266+
for (i = 0; deltaenv[i]; i++) {
1267+
size_t s = strlen(deltaenv[i]) + 1;
1268+
delta_size += s;
1269+
}
1270+
1271+
ALLOC_ARRAY(wdeltaenv, delta_size);
1272+
1273+
/* convert the deltaenv, appending to array */
1274+
for (i = 0, p3 = wdeltaenv; deltaenv[i]; i++) {
1275+
size_t s = strlen(deltaenv[i]) + 1, wlen;
1276+
wlen = xutftowcs(p3, deltaenv[i], s * 2);
1277+
1278+
ALLOC_GROW(array, nr + 1, alloc);
1279+
array[nr++] = p3;
1280+
1281+
p3 += wlen + 1;
1282+
}
1283+
1284+
git_qsort(array, nr, sizeof(*array), wenvcmp);
1285+
ALLOC_ARRAY(result, size + delta_size);
1286+
1287+
for (p3 = result, i = 0; i < nr; i++) {
1288+
wchar_t *equal = wcschr(array[i], L'=');;
1289+
1290+
/* Skip "to delete" entry */
1291+
if (!equal)
1292+
continue;
1293+
1294+
p = array[i];
1295+
1296+
/* Skip any duplicate */
1297+
if (i + 1 < nr) {
1298+
wchar_t *next = array[i + 1];
1299+
size_t n = equal - p;
1300+
1301+
if (!_wcsnicmp(p, next, n) && (!next[n] || next[n] == L'='))
1302+
continue;
1303+
}
1304+
1305+
size = wcslen(p) + 1;
1306+
memcpy(p3, p, size * sizeof(*p));
1307+
p3 += size;
1308+
}
1309+
*p3 = L'\0';
1310+
1311+
free(array);
1312+
FreeEnvironmentStringsW(wenv);
1313+
return result;
1314+
}
1315+
1316+
#else
1317+
11771318
static int do_putenv(char **env, const char *name, int size, int free_old);
11781319

11791320
/* used number of elements of environ array, including terminating NULL */
@@ -1220,6 +1361,7 @@ static wchar_t *make_environment_block(char **deltaenv)
12201361
free(tmpenv);
12211362
return wenvblk;
12221363
}
1364+
#endif
12231365

12241366
static void do_unset_environment_variables(void)
12251367
{
@@ -1510,6 +1652,70 @@ int mingw_kill(pid_t pid, int sig)
15101652
return -1;
15111653
}
15121654

1655+
#if defined(_MSC_VER)
1656+
1657+
/* UTF8 versions of getenv and putenv (and unsetenv).
1658+
* Internally, they use the CRT's stock UNICODE routines
1659+
* to avoid data loss.
1660+
*
1661+
* Unlike the mingw version, we DO NOT directly write to
1662+
* the CRT variables. We also DO NOT try to manage/replace
1663+
* the CRT storage.
1664+
*/
1665+
char *msc_getenv(const char *name)
1666+
{
1667+
int len_key, len_value;
1668+
wchar_t *w_key;
1669+
char *value;
1670+
const wchar_t *w_value;
1671+
1672+
if (!name || !*name)
1673+
return NULL;
1674+
1675+
len_key = strlen(name) + 1;
1676+
w_key = calloc(len_key, sizeof(wchar_t));
1677+
xutftowcs(w_key, name, len_key);
1678+
w_value = _wgetenv(w_key);
1679+
free(w_key);
1680+
1681+
if (!w_value)
1682+
return NULL;
1683+
1684+
len_value = wcslen(w_value) * 3 + 1;
1685+
value = calloc(len_value, sizeof(char));
1686+
xwcstoutf(value, w_value, len_value);
1687+
1688+
/* TODO Warning: We return "value" which is an allocated
1689+
* value and the caller is NOT expecting to have to free
1690+
* it, so we leak memory.
1691+
*/
1692+
return value;
1693+
}
1694+
1695+
int msc_putenv(const char *name)
1696+
{
1697+
int len, result;
1698+
char *equal;
1699+
wchar_t *wide;
1700+
1701+
if (!name || !*name)
1702+
return 0;
1703+
1704+
len = strlen(name);
1705+
equal = strchr(name, '=');
1706+
wide = calloc(len+1+!equal, sizeof(wchar_t));
1707+
xutftowcs(wide, name, len+1);
1708+
if (!equal)
1709+
wcscat(wide, L"=");
1710+
1711+
result = _wputenv(wide);
1712+
1713+
free(wide);
1714+
return result;
1715+
}
1716+
1717+
#else
1718+
15131719
/*
15141720
* Compare environment entries by key (i.e. stopping at '=' or '\0').
15151721
*/
@@ -1638,6 +1844,8 @@ int mingw_putenv(const char *namevalue)
16381844
return 0;
16391845
}
16401846

1847+
#endif
1848+
16411849
/*
16421850
* Note, this isn't a complete replacement for getaddrinfo. It assumes
16431851
* that service contains a numerical port, or that it is null. It

compat/mingw.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,53 @@ char *mingw_getcwd(char *pointer, int len);
265265
#error "NO_UNSETENV is incompatible with the MinGW startup code!"
266266
#endif
267267

268+
#if defined(_MSC_VER)
269+
/*
270+
* We bind *env() routines (even the mingw_ ones) to private msc_ versions.
271+
* These talk to the CRT using UNICODE/wchar_t, but maintain the original
272+
* narrow-char API.
273+
*
274+
* Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv())
275+
* routines and stores both versions of each environment variable in parallel
276+
* (and secretly updates both when you set one or the other), but it uses CP_ACP
277+
* to do the conversion rather than CP_UTF8.
278+
*
279+
* Since everything in the git code base is UTF8, we define the msc_ routines
280+
* to access the CRT using the UNICODE routines and manually convert them to
281+
* UTF8. This also avoids round-trip problems.
282+
*
283+
* This also helps with our linkage, since "_wenviron" is publicly exported
284+
* from the CRT. But to access "_environ" we would have to statically link
285+
* to the CRT (/MT).
286+
*
287+
* We also use "wmain(argc,argv,env)" and get the initial UNICODE setup for us.
288+
* This avoids the need for the msc_startup() to import and convert the
289+
* inherited environment.
290+
*
291+
* We require NO_SETENV (and let gitsetenv() call our msc_putenv).
292+
*/
293+
#define getenv msc_getenv
294+
#define putenv msc_putenv
295+
#define unsetenv msc_putenv
296+
#define mingw_getenv msc_getenv
297+
#define mingw_putenv msc_putenv
298+
char *msc_getenv(const char *name);
299+
int msc_putenv(const char *name);
300+
301+
#ifndef NO_SETENV
302+
#error "NO_SETENV is required for MSC startup code!"
303+
#endif
304+
305+
#else
306+
268307
char *mingw_getenv(const char *name);
269308
#define getenv mingw_getenv
270309
int mingw_putenv(const char *namevalue);
271310
#define putenv mingw_putenv
272311
#define unsetenv mingw_putenv
273312

313+
#endif
314+
274315
int mingw_gethostname(char *host, int namelen);
275316
#define gethostname mingw_gethostname
276317

0 commit comments

Comments
 (0)