Skip to content

Commit d4d04e6

Browse files
committed
UTF-8 environment: be a little bit more defensive
It is unlikely that we have an empty environment, ever, but *if* we do, when `environ_size - 1` is passed to `bsearchenv()` it is misinterpreted as a real large integer. To make the code truly defensive, refuse to do anything at all if the size is negative (which should not happen, of course). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 1c49d14 commit d4d04e6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

compat/mingw.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ static int bsearchenv(char **env, const char *name, size_t size)
12921292
*/
12931293
static int do_putenv(char **env, const char *name, int size, int free_old)
12941294
{
1295-
int i = bsearchenv(env, name, size - 1);
1295+
int i = size <= 0 ? -1 : bsearchenv(env, name, size - 1);
12961296

12971297
/* optionally free removed / replaced entry */
12981298
if (i >= 0 && free_old)
@@ -1317,7 +1317,13 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
13171317
char *mingw_getenv(const char *name)
13181318
{
13191319
char *value;
1320-
int pos = bsearchenv(environ, name, environ_size - 1);
1320+
int pos;
1321+
1322+
if (environ_size <= 0)
1323+
return NULL;
1324+
1325+
pos = bsearchenv(environ, name, environ_size - 1);
1326+
13211327
if (pos < 0)
13221328
return NULL;
13231329
value = strchr(environ[pos], '=');

0 commit comments

Comments
 (0)