D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15783 - Junk is written into environment variable after assigning null to it
Summary: Junk is written into environment variable after assigning null to it
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Linux
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-10 11:02 UTC by Roman
Modified: 2017-06-25 10:21 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Roman 2016-03-10 11:02:43 UTC
import std.stdio;
import std.process;
import std.path;

void test(string envvar)
{   
    environment[envvar] = "myvalue";
    writefln("check env: %s=%s (%s)", envvar, environment.get(envvar), environment.get(envvar).length);
    environment[envvar] = null;
    writefln("check env: %s=%s (%s)", envvar, environment.get(envvar), environment.get(envvar).length);
}

void main(string[] args)
{
    test("TEST_VAR1");
    test("TEST_VAR2");
}

This seems to be random. Sometimes it writes junk only to the second variable.
Comment 1 e10s 2016-03-18 15:04:06 UTC
Test case:

import std.conv;
import std.stdio;
import std.string;
import core.sys.posix.stdlib;

void main()
{
    immutable key = "AAAAAAAA".toStringz;
    foreach (i; 0 .. 100)
    {
        setenv(key, null, 1);
        const r = getenv(key);
        if (r && *r)
        {
            writefln("%s %s %s", i, r, r.to!string);
        }
    }
}

Result:
http://melpon.org/wandbox/permlink/cqfITPRJMSKOD60q

It's terrible. What does getenv return?
Comment 2 Simen Kjaeraas 2016-03-18 15:29:04 UTC
core.sys.posix.stdlib.setenv is a simple wrapper around the posix function of the same name, and should have the same behavior. This is not a bug in core.sys.posix.stdlib, but one might argue there is one in phobos' std.process.environment.

The OS is simply following the standard, which says nothing about how to handle nulls:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
Comment 3 Roman 2017-04-03 23:20:06 UTC
Still issue for 2.073.2

Can we just decide that providing the null means removing environment variable or setting it to empty string? Better make it behave the same way as on Windows (not sure what's behavior though).
Comment 4 Roman 2017-06-25 09:59:36 UTC
Was fixed in https://github.com/dlang/phobos/pull/5324
Can we get this closed?
Comment 5 Seb 2017-06-25 10:21:46 UTC
@Roman: If you reference the Bugzilla issue(s) in your PRs, they will be automatically closed:

https://github.com/dlang-bots/dlang-bot#automated-references

There are other advantages of doing this as well, e.g. referenced issues will be listed on the changelog.