Skip to content

Commit 66d2714

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 24f9b07 commit 66d2714

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
@@ -1239,7 +1239,7 @@ static int bsearchenv(char **env, const char *name, size_t size)
12391239
*/
12401240
static int do_putenv(char **env, const char *name, int size, int free_old)
12411241
{
1242-
int i = bsearchenv(env, name, size - 1);
1242+
int i = size <= 0 ? -1 : bsearchenv(env, name, size - 1);
12431243

12441244
/* optionally free removed / replaced entry */
12451245
if (i >= 0 && free_old)
@@ -1264,7 +1264,13 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
12641264
char *mingw_getenv(const char *name)
12651265
{
12661266
char *value;
1267-
int pos = bsearchenv(environ, name, environ_size - 1);
1267+
int pos;
1268+
1269+
if (environ_size <= 0)
1270+
return NULL;
1271+
1272+
pos = bsearchenv(environ, name, environ_size - 1);
1273+
12681274
if (pos < 0)
12691275
return NULL;
12701276
value = strchr(environ[pos], '=');

0 commit comments

Comments
 (0)