Skip to content

Commit

Permalink
Combined revert of Environment Block Changes
Browse files Browse the repository at this point in the history
Revert "Fix environment block creation (#7401)"

This reverts commit 7886f16.

(cherry picked from commit e46ba65)

Revert "Always create a new environment block before we spawn a process (#7243)"

This reverts commit 849243a.

References #7418

(cherry picked from commit 4204d25)
(cherry picked from commit f8e8572)
(cherry picked from commit cb4c4f7)
(cherry picked from commit afb0cac)
(cherry picked from commit b25dc74)
(cherry picked from commit 5f7c66b)
  • Loading branch information
DHowett committed Jan 11, 2022
1 parent d5b177d commit 3c6622c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
8 changes: 2 additions & 6 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "ConptyConnection.h"

#include <UserEnv.h>
#include <winternl.h>

#include "ConptyConnection.g.cpp"
Expand Down Expand Up @@ -99,11 +98,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 '{}'.
Expand Down
38 changes: 12 additions & 26 deletions src/types/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wchar_t const*>(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) };
Expand Down
5 changes: 1 addition & 4 deletions src/types/inc/Environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ namespace Microsoft::Console::Utils
}
};

using EnvironmentBlockPtr = wil::unique_any<void*, decltype(::DestroyEnvironmentBlock), ::DestroyEnvironmentBlock>;
[[nodiscard]] EnvironmentBlockPtr CreateEnvironmentBlock();

using EnvironmentVariableMapW = std::map<std::wstring, std::wstring, WStringCaseInsensitiveCompare>;

[[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock = nullptr) noexcept;
[[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept;

[[nodiscard]] HRESULT EnvironmentMapToEnvironmentStringsW(EnvironmentVariableMapW& map,
std::vector<wchar_t>& newEnvVars) noexcept;
Expand Down
1 change: 0 additions & 1 deletion src/types/precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Module Name:

// Windows Header Files:
#include <windows.h>
#include <userenv.h>
#include <combaseapi.h>
#include <UIAutomation.h>
#include <objbase.h>
Expand Down

0 comments on commit 3c6622c

Please sign in to comment.