Skip to content

Commit

Permalink
Prefer the first, not last, of any env var duplicates
Browse files Browse the repository at this point in the history
If envp contains duplicate environment variables, use the
first value, not the last value. Fixes #2784.
  • Loading branch information
ridiculousfish committed Mar 6, 2016
1 parent 1e7c3fe commit 3044697
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,16 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
is to insert valid data
*/

/*
Import environment variables
*/
for (char **p = (environ ? environ : __environ); p && *p; p++)
/* Import environment variables. Walk backwards so that the first one out of any duplicates wins (#2784) */
const char * const * envp = (environ ? environ : __environ);
size_t i = 0;
while (envp && envp[i])
{
i++;
}
while (i--)
{
const wcstring key_and_val = str2wcstring(*p); //like foo=bar
const wcstring key_and_val = str2wcstring(envp[i]); //like foo=bar
size_t eql = key_and_val.find(L'=');
if (eql == wcstring::npos)
{
Expand Down

0 comments on commit 3044697

Please sign in to comment.