Skip to content

Commit 4be636e

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 7ae7d11 commit 4be636e

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
@@ -1347,7 +1347,7 @@ static int bsearchenv(char **env, const char *name, size_t size)
13471347
*/
13481348
static int do_putenv(char **env, const char *name, int size, int free_old)
13491349
{
1350-
int i = bsearchenv(env, name, size - 1);
1350+
int i = size <= 0 ? -1 : bsearchenv(env, name, size - 1);
13511351

13521352
/* optionally free removed / replaced entry */
13531353
if (i >= 0 && free_old)
@@ -1372,7 +1372,13 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
13721372
char *mingw_getenv(const char *name)
13731373
{
13741374
char *value;
1375-
int pos = bsearchenv(environ, name, environ_size - 1);
1375+
int pos;
1376+
1377+
if (environ_size <= 0)
1378+
return NULL;
1379+
1380+
pos = bsearchenv(environ, name, environ_size - 1);
1381+
13761382
if (pos < 0)
13771383
return NULL;
13781384
value = strchr(environ[pos], '=');

0 commit comments

Comments
 (0)