Skip to content

Commit 03aeafd

Browse files
authored
Merge pull request #10616 from ibuclaw/merge_stable
Fix #10574 - environment pointer sometimes changes before exec, causing segfault in child process.
2 parents 63fdb28 + d643650 commit 03aeafd

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

std/process.d

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ private Pid spawnProcessPosix(scope const(char[])[] args,
12051205
}
12061206

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

12101210
// If execution fails, exit as quickly as possible.
12111211
abortOnError(forkPipeOut, InternalError.exec, .errno);
@@ -1511,7 +1511,7 @@ private Pid spawnProcessWin(scope const(char)[] commandLine,
15111511
// on the form "name=value", optionally adding those of the current process'
15121512
// environment strings that are not present in childEnv. If the parent's
15131513
// environment should be inherited without modification, this function
1514-
// returns environ directly.
1514+
// returns null.
15151515
version (Posix)
15161516
private const(char*)* createEnv(const string[string] childEnv,
15171517
bool mergeWithParentEnv)
@@ -1521,7 +1521,7 @@ private const(char*)* createEnv(const string[string] childEnv,
15211521
auto environ = getEnvironPtr;
15221522
if (mergeWithParentEnv)
15231523
{
1524-
if (childEnv.length == 0) return environ;
1524+
if (childEnv.length == 0) return null;
15251525
while (environ[parentEnvLength] != null) ++parentEnvLength;
15261526
}
15271527

@@ -1551,16 +1551,7 @@ version (Posix) @system unittest
15511551
assert(e1 != null && *e1 == null);
15521552

15531553
auto e2 = createEnv(null, true);
1554-
assert(e2 != null);
1555-
int i = 0;
1556-
auto environ = getEnvironPtr;
1557-
for (; environ[i] != null; ++i)
1558-
{
1559-
assert(e2[i] != null);
1560-
import core.stdc.string : strcmp;
1561-
assert(strcmp(e2[i], environ[i]) == 0);
1562-
}
1563-
assert(e2[i] == null);
1554+
assert(e2 == null);
15641555

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

0 commit comments

Comments
 (0)