aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/env.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-03-05 19:02:50 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-03-05 19:02:50 -0800
commit3044697baa8e098a31f8049194d7cf1890363cb2 (patch)
tree18802532c733b9dc784ae32e3fe9b388da12674c /src/env.cpp
parent1e7c3fe70964711918bcf13553ae9fb66ed73a17 (diff)
Prefer the first, not last, of any env var duplicates
If envp contains duplicate environment variables, use the first value, not the last value. Fixes #2784.
Diffstat (limited to 'src/env.cpp')
-rw-r--r--src/env.cpp14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/env.cpp b/src/env.cpp
index e6deec52..28ea51b0 100644
--- a/src/env.cpp
+++ b/src/env.cpp
@@ -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)
{