Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Process.GetEnvironmentVariablesBlock #49484

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -863,30 +863,23 @@ private void CreatePipe(out SafeFileHandle parentHandle, out SafeFileHandle chil

private static string GetEnvironmentVariablesBlock(IDictionary<string, string> sd)
{
// get the keys
string[] keys = new string[sd.Count];
sd.Keys.CopyTo(keys, 0);

// sort both by the keys
// Windows 2000 requires the environment block to be sorted by the key
// It will first converting the case the strings and do ordinal comparison.
// https://docs.microsoft.com/en-us/windows/win32/procthread/changing-environment-variables
// "All strings in the environment block must be sorted alphabetically by name. The sort is
// case-insensitive, Unicode order, without regard to locale. Because the equal sign is a
// separator, it must not be used in the name of an environment variable."

// We do not use Array.Sort(keys, values, IComparer) since it is only supported
// in System.Runtime contract from 4.20.0.0 and Test.Net depends on System.Runtime 4.0.10.0
// we workaround this by sorting only the keys and then lookup the values form the keys.
var keys = new string[sd.Count];
sd.Keys.CopyTo(keys, 0);
Array.Sort(keys, StringComparer.OrdinalIgnoreCase);

// create a list of null terminated "key=val" strings
StringBuilder stringBuff = new StringBuilder();
for (int i = 0; i < sd.Count; ++i)
// Join the null-terminated "key=val\0" strings
var result = new StringBuilder();
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
foreach (string key in keys)
{
stringBuff.Append(keys[i]);
stringBuff.Append('=');
stringBuff.Append(sd[keys[i]]);
stringBuff.Append('\0');
result.Append(key).Append('=').Append(sd[key]).Append('\0');
}
// an extra null at the end that indicates end of list will come from the string.
return stringBuff.ToString();

return result.ToString();
}
}
}