Spring.Utils|TEnvironment.SetEnvironmentVariable documentation mismatch and possible AV

Issue #40 resolved
Former user created an issue

In order to remove an environment variable, you have to pass a NULL pointer to the WinApi/SetEnvironmentVariable [1] function. TEnvironment.SetEnvironmentVariable expects a string. That string is casted to a PChar. Even if the given string is empty, the PChar it is casted to will never be a NULL pointer. This way the environment variables cannot be removed with Spring, only set to an empty string. So the documentation is wrong when it states variables can be removed.

Furthermore, when reading out an environment variable that is an empty string (a PChar to a #0 character), the inner function GetProcessEnvironmentVariable will trigger an AV because it is calling

SetLength(Result, 0)

followed by

Windows.GetEnvironmentVariable(PChar(variable), PChar(Result), len);

Attached is a minimal example. Please note that I only used the method overloads for using TEnvironmentVariableTarget.evtProcess. evtUser and evtMachine were not tested.

[1] http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx

Comments (6)

  1. Log in to comment