From 5f7c66bc0cc5d3f217a723ee7f92904fec6be91d Mon Sep 17 00:00:00 2001 From: Dustin Howett Date: Mon, 21 Sep 2020 11:02:44 -0700 Subject: [PATCH] [STABLE ONLY] Combined revert of Environment Block Changes Revert "Fix environment block creation (#7401)" This reverts commit 7886f16714a734fe1a122b9f22986d283d0f0a41. (cherry picked from commit e46ba65665610f2544414618b8a0de00c03e7903) Revert "Always create a new environment block before we spawn a process (#7243)" This reverts commit 849243af995a05bcce046df010894b60d20ea145. References #7418 (cherry picked from commit 4204d2535c82ebeef4f27c306b39ffbcca51a881) (cherry picked from commit f8e8572c2314004da9c355ef4bfec33a56d54646) (cherry picked from commit cb4c4f7b739708a4908067918f3e3f31bce1cfbf) (cherry picked from commit afb0cac3e323f80d4df4804be2155bf317d8c33d) (cherry picked from commit b25dc74a1d0ef47cf3d2fe599b9f788e89ccbc2f) --- .../TerminalConnection/ConptyConnection.cpp | 8 +--- src/types/Environment.cpp | 38 ++++++------------- src/types/inc/Environment.hpp | 5 +-- src/types/precomp.h | 1 - 4 files changed, 15 insertions(+), 37 deletions(-) diff --git a/src/cascadia/TerminalConnection/ConptyConnection.cpp b/src/cascadia/TerminalConnection/ConptyConnection.cpp index a6dcbd3b435..5a84f14e2f1 100644 --- a/src/cascadia/TerminalConnection/ConptyConnection.cpp +++ b/src/cascadia/TerminalConnection/ConptyConnection.cpp @@ -5,7 +5,6 @@ #include "ConptyConnection.h" -#include #include #include "ConptyConnection.g.cpp" @@ -165,11 +164,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation environment.clear(); }); - { - const auto newEnvironmentBlock{ Utils::CreateEnvironmentBlock() }; - // Populate the environment map with the current environment. - RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment, newEnvironmentBlock.get())); - } + // Populate the environment map with the current environment. + RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment)); { // Convert connection Guid to string and ignore the enclosing '{}'. diff --git a/src/types/Environment.cpp b/src/types/Environment.cpp index 962297ace2c..106503d8404 100644 --- a/src/types/Environment.cpp +++ b/src/types/Environment.cpp @@ -3,50 +3,36 @@ #include "precomp.h" #include "inc/Environment.hpp" -#include "wil/token_helpers.h" using namespace ::Microsoft::Console::Utils; // We cannot use spand or not_null because we're dealing with \0\0-terminated buffers of unknown length #pragma warning(disable : 26481 26429) -// Function Description: -// - Wraps win32's CreateEnvironmentBlock to return a smart pointer. -EnvironmentBlockPtr Microsoft::Console::Utils::CreateEnvironmentBlock() -{ - void* newEnvironmentBlock{ nullptr }; - auto processToken{ wil::open_current_access_token(TOKEN_QUERY | TOKEN_DUPLICATE) }; - if (!::CreateEnvironmentBlock(&newEnvironmentBlock, processToken.get(), FALSE)) - { - return nullptr; - } - return EnvironmentBlockPtr{ newEnvironmentBlock }; -} - // Function Description: // - Updates an EnvironmentVariableMapW with the current process's unicode // environment variables ignoring ones already set in the provided map. // Arguments: // - map: The map to populate with the current processes's environment variables. -// - environmentBlock: Optional environment block to use when filling map. If omitted, -// defaults to the current environment. // Return Value: // - S_OK if we succeeded, or an appropriate HRESULT for failing -HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock) noexcept +HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept try { - wchar_t const* activeEnvironmentBlock{ static_cast(environmentBlock) }; + LPWCH currentEnvVars{}; + auto freeCurrentEnv = wil::scope_exit([&] { + if (currentEnvVars) + { + FreeEnvironmentStringsW(currentEnvVars); + currentEnvVars = nullptr; + } + }); - wil::unique_environstrings_ptr currentEnvVars; - if (!activeEnvironmentBlock) - { - currentEnvVars.reset(::GetEnvironmentStringsW()); - RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars); - activeEnvironmentBlock = currentEnvVars.get(); - } + currentEnvVars = ::GetEnvironmentStringsW(); + RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars); // Each entry is NULL-terminated; block is guaranteed to be double-NULL terminated at a minimum. - for (wchar_t const* lastCh{ activeEnvironmentBlock }; *lastCh != '\0'; ++lastCh) + for (wchar_t const* lastCh{ currentEnvVars }; *lastCh != '\0'; ++lastCh) { // Copy current entry into temporary map. const size_t cchEntry{ ::wcslen(lastCh) }; diff --git a/src/types/inc/Environment.hpp b/src/types/inc/Environment.hpp index a982db60432..0994a177762 100644 --- a/src/types/inc/Environment.hpp +++ b/src/types/inc/Environment.hpp @@ -21,12 +21,9 @@ namespace Microsoft::Console::Utils } }; - using EnvironmentBlockPtr = wil::unique_any; - [[nodiscard]] EnvironmentBlockPtr CreateEnvironmentBlock(); - using EnvironmentVariableMapW = std::map; - [[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock = nullptr) noexcept; + [[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept; [[nodiscard]] HRESULT EnvironmentMapToEnvironmentStringsW(EnvironmentVariableMapW& map, std::vector& newEnvVars) noexcept; diff --git a/src/types/precomp.h b/src/types/precomp.h index d14fc703a8d..0b4a1124db6 100644 --- a/src/types/precomp.h +++ b/src/types/precomp.h @@ -29,7 +29,6 @@ Module Name: // Windows Header Files: #include -#include #include #include #include