Skip to content

Commit

Permalink
Fix bugzilla issue 24549 -- environment.get(null) segfaults on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
schveiguy authored and dlang-bot committed May 16, 2024
1 parent fcccc61 commit 82e8e32
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions std/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ static:
*/
bool opBinaryRight(string op : "in")(scope const(char)[] name) @trusted
{
if (name is null)
return false;
version (Posix)
return core.sys.posix.stdlib.getenv(name.tempCString()) !is null;
else version (Windows)
Expand Down Expand Up @@ -451,6 +453,10 @@ private:
// doesn't exist.
void getImpl(scope const(char)[] name, scope void delegate(const(OSChar)[]) @safe sink) @trusted
{
// fix issue https://issues.dlang.org/show_bug.cgi?id=24549
if (name is null)
return sink(null);

version (Windows)
{
// first we ask windows how long the environment variable is,
Expand Down Expand Up @@ -600,6 +606,15 @@ private:
assert("std_process" !in environment);
}

// https://issues.dlang.org/show_bug.cgi?id=24549
@safe unittest
{
import std.exception : assertThrown;
assert(environment.get(null) is null);
assertThrown(environment[null]);
assert(!(null in environment));
}

// =============================================================================
// Functions and classes for process management.
// =============================================================================
Expand Down

0 comments on commit 82e8e32

Please sign in to comment.