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

Fix #10574 - environment pointer sometimes changes before exec, causing segfault in child process. #10615

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 7 additions & 16 deletions std/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ private
{
version (Darwin)
{
extern(C) char*** _NSGetEnviron() nothrow;
const(char**) getEnvironPtr() @trusted
extern(C) char*** _NSGetEnviron() @nogc nothrow;
const(char**) getEnvironPtr() @trusted @nogc nothrow
{
return *_NSGetEnviron;
}
Expand All @@ -158,7 +158,7 @@ private
{
// Made available by the C runtime:
extern(C) extern __gshared const char** environ;
const(char**) getEnvironPtr() @trusted
const(char**) getEnvironPtr() @trusted @nogc nothrow
{
return environ;
}
Expand Down Expand Up @@ -1221,7 +1221,7 @@ private Pid spawnProcessPosix(scope const(char[])[] args,
}

// Execute program.
core.sys.posix.unistd.execve(argz[0], argz.ptr, envz);
core.sys.posix.unistd.execve(argz[0], argz.ptr, envz is null ? getEnvironPtr : envz);

// If execution fails, exit as quickly as possible.
abortOnError(forkPipeOut, InternalError.exec, .errno);
Expand Down Expand Up @@ -1527,7 +1527,7 @@ private Pid spawnProcessWin(scope const(char)[] commandLine,
// on the form "name=value", optionally adding those of the current process'
// environment strings that are not present in childEnv. If the parent's
// environment should be inherited without modification, this function
// returns environ directly.
// returns null.
version (Posix)
private const(char*)* createEnv(const string[string] childEnv,
bool mergeWithParentEnv)
Expand All @@ -1537,7 +1537,7 @@ private const(char*)* createEnv(const string[string] childEnv,
auto environ = getEnvironPtr;
if (mergeWithParentEnv)
{
if (childEnv.length == 0) return environ;
if (childEnv.length == 0) return null;
while (environ[parentEnvLength] != null) ++parentEnvLength;
}

Expand Down Expand Up @@ -1567,16 +1567,7 @@ version (Posix) @system unittest
assert(e1 != null && *e1 == null);

auto e2 = createEnv(null, true);
assert(e2 != null);
int i = 0;
auto environ = getEnvironPtr;
for (; environ[i] != null; ++i)
{
assert(e2[i] != null);
import core.stdc.string : strcmp;
assert(strcmp(e2[i], environ[i]) == 0);
}
assert(e2[i] == null);
assert(e2 == null);

auto e3 = createEnv(["foo" : "bar", "hello" : "world"], false);
assert(e3 != null && e3[0] != null && e3[1] != null && e3[2] == null);
Expand Down