Bug 15783 – Junk is written into environment variable after assigning null to it

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2016-03-10T11:02:00Z
Last change time
2017-06-25T10:21:46Z
Assigned to
nobody
Creator
freeslave93

Comments

Comment #0 by freeslave93 — 2016-03-10T11:02:43Z
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 by electrolysis.jp+d — 2016-03-18T15:04:06Z
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 by simen.kjaras — 2016-03-18T15:29:04Z
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 by freeslave93 — 2017-04-03T23:20:06Z
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 by freeslave93 — 2017-06-25T09:59:36Z
Was fixed in https://github.com/dlang/phobos/pull/5324 Can we get this closed?
Comment #5 by greensunny12 — 2017-06-25T10:21:46Z
@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.