Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't interleave iterating over environ with unsetting environment variables #1315

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/miral/launch_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,36 @@

#include <stdexcept>
#include <cstring>
#include <vector>

namespace
{
void strip_mir_env_variables()
{
static char const mir_prefix[] = "MIR_";

std::vector<std::string> vars_to_remove;

for (auto var = environ; *var; ++var)
{
auto const var_begin = *var;
if (strncmp(var_begin, mir_prefix, sizeof(mir_prefix) - 1) == 0)
{
if (auto var_end = strchr(var_begin, '='))
{
unsetenv(std::string(var_begin, var_end).c_str());
vars_to_remove.emplace_back(var_begin, var_end);
}
else
{
unsetenv(var_begin);
vars_to_remove.emplace_back(var_begin);
}
Comment on lines 41 to 48
Copy link
Collaborator

@Saviq Saviq Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future's sake:

<Saviq> alan_g: for my sake, IIUC some elements of `environ` have equal signs, some don't? it's because some of them will have values, other will just be defined?
<alan_g> Saviq, exactly
<alan_g> Or rather, they all seem to have "=" but the docs don't seem to guarantee it
<Saviq> hmm, "by convention"
<alan_g> Actually, I guess "=" is required. I was just paranoid.
<alan_g> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
<alan_g> (But that's pre-existing.)

+1 on this fixing the problem of removal from a moving target.

bors r+

}
}

for (auto const& var : vars_to_remove)
{
unsetenv(var.c_str());
}
}
}

Expand Down