Fix bugzilla issue 24549 -- environment.get(null) segfaults on Linux

This commit is contained in:
Steven Schveighoffer 2024-05-13 21:10:06 -04:00 committed by The Dlang Bot
parent fcccc61b95
commit 82e8e32b2f

View file

@ -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)
@ -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,
@ -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.
// =============================================================================