Repository: microsoft/terminal
File: src/inc/til/env.h (C/C++)

Code before:
                                    data.assign(p, l);
                                }
                                else if (pass == 1 && (type == REG_EXPAND_SZ) && valueDataSize >= sizeof(wchar_t))
                                {
                                    const auto p = reinterpret_cast<wchar_t*>(valueData.data());
                                    const auto l = wcsnlen(p, valueData.size() / sizeof(wchar_t));
                                    data.assign(p, l);
                                    data = expand_environment_strings(data.data());
                                }

                                if (!data.empty())
                                {
                                    if (isPathVar)
                                    {
                                        concat_var(valueName, std::move(data));
                                    }
                                    else
                                    {
                                        set_user_environment_var(valueName, std::move(data));
                                    }
                                }
                            }

                            valueName.resize(maxValueNameSize);
                            valueData.resize(maxValueDataSize);
                            valueNameSize = maxValueNameSize;
                            valueDataSize = maxValueDataSize;
                            index++;
                        }
                        index = 0;
                    }
                }
            }
        }

        std::wstring expand_environment_strings(std::wstring_view input)
        {
            std::wstring expanded;
            expanded.reserve(input.size());
            bool isInEnvVarName = false;
            std::wstring currentEnvVarName;
            for (const auto character : input)
            {
                if (character == L'%')
                {
                    if (isInEnvVarName)
                    {
                        if (const auto envVarValue = _envMap.find(currentEnvVarName); envVarValue != _envMap.end())
                        {
                            expanded.append(envVarValue->second);
                        }

Review comment (suggestion):
My intention was to just remove these two lines here, but then I realized that it doesn't actually seem to do anything, because `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment` contains a `USERNAME` value (with the value-value of `SYSTEM`) which immediately overwrites this. Reading more into [volatile environment variables](https://ss64.com/nt/syntax-variables.html), I've realized that this code can be replaced with one that simply copies them from the stale (process) environment block, because they are immutable across a login.

Code after:
                                    data.assign(p, l);
                                }
                                else if (pass == 1 && (type == REG_EXPAND_SZ) && valueDataSize >= sizeof(wchar_t))
                                {
                                    const auto p = reinterpret_cast<const wchar_t*>(valueData.data());
                                    const auto l = wcsnlen(p, valueData.size() / sizeof(wchar_t));
                                    data.assign(p, l);
                                    data = expand_environment_strings(data.data());
                                }

                                if (!data.empty())
                                {
                                    if (isPathVar)
                                    {
                                        concat_var(valueName, std::move(data));
                                    }
                                    else
                                    {
                                        set_user_environment_var(valueName, std::move(data));
                                    }
                                }
                            }

                            valueName.resize(maxValueNameSize);
                            valueData.resize(maxValueDataSize);
                            valueNameSize = maxValueNameSize;
                            valueDataSize = maxValueDataSize;
                            index++;
                        }
                        index = 0;
                    }
                }
            }
        }

        std::wstring expand_environment_strings(std::wstring_view input)
        {
            std::wstring expanded;
            expanded.reserve(input.size());
            bool isInEnvVarName = false;
            std::wstring currentEnvVarName;
            for (const auto character : input)
            {
                if (character == L'%')
                {
                    if (isInEnvVarName)
                    {
                        if (const auto envVarValue = _envMap.find(currentEnvVarName); envVarValue != _envMap.end())
                        {
                            expanded.append(envVarValue->second);
                        }