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 <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2015-03-17 19:41:02 +01:00
committed by Jameson Miller
parent fd7eb8c6c2
commit 3cd10cf43d

View File

@@ -1451,7 +1451,7 @@ static int bsearchenv(char **env, const char *name, size_t size)
*/
static int do_putenv(char **env, const char *name, int size, int free_old)
{
int i = bsearchenv(env, name, size - 1);
int i = size <= 0 ? -1 : bsearchenv(env, name, size - 1);
/* optionally free removed / replaced entry */
if (i >= 0 && free_old)
@@ -1476,7 +1476,13 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
char *mingw_getenv(const char *name)
{
char *value;
int pos = bsearchenv(environ, name, environ_size - 1);
int pos;
if (environ_size <= 0)
return NULL;
pos = bsearchenv(environ, name, environ_size - 1);
if (pos < 0)
return NULL;
value = strchr(environ[pos], '=');