Skip to content

Commit b2d2e0a

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 e37b037 commit b2d2e0a

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

13551355
/* optionally free removed / replaced entry */
13561356
if (i >= 0 && free_old)
@@ -1375,7 +1375,13 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
13751375
char *mingw_getenv(const char *name)
13761376
{
13771377
char *value;
1378-
int pos = bsearchenv(environ, name, environ_size - 1);
1378+
int pos;
1379+
1380+
if (environ_size <= 0)
1381+
return NULL;
1382+
1383+
pos = bsearchenv(environ, name, environ_size - 1);
1384+
13791385
if (pos < 0)
13801386
return NULL;
13811387
value = strchr(environ[pos], '=');

0 commit comments

Comments
 (0)