Skip to content

Commit b25dc74

Browse files
committed
[STABLE ONLY] Combined revert of Environment Block Changes
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)
1 parent 2366427 commit b25dc74

File tree

4 files changed

+15
-37
lines changed

4 files changed

+15
-37
lines changed

src/cascadia/TerminalConnection/ConptyConnection.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "ConptyConnection.h"
77

88
#include <windows.h>
9-
#include <userenv.h>
109

1110
#include "ConptyConnection.g.cpp"
1211
#include "CTerminalHandoff.h"
@@ -95,11 +94,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
9594
environment.clear();
9695
});
9796

98-
{
99-
const auto newEnvironmentBlock{ Utils::CreateEnvironmentBlock() };
100-
// Populate the environment map with the current environment.
101-
RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment, newEnvironmentBlock.get()));
102-
}
97+
// Populate the environment map with the current environment.
98+
RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment));
10399

104100
{
105101
// Convert connection Guid to string and ignore the enclosing '{}'.

src/types/Environment.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,36 @@
33

44
#include "precomp.h"
55
#include "inc/Environment.hpp"
6-
#include "wil/token_helpers.h"
76

87
using namespace ::Microsoft::Console::Utils;
98

109
// We cannot use spand or not_null because we're dealing with \0\0-terminated buffers of unknown length
1110
#pragma warning(disable : 26481 26429)
1211

13-
// Function Description:
14-
// - Wraps win32's CreateEnvironmentBlock to return a smart pointer.
15-
EnvironmentBlockPtr Microsoft::Console::Utils::CreateEnvironmentBlock()
16-
{
17-
void* newEnvironmentBlock{ nullptr };
18-
auto processToken{ wil::open_current_access_token(TOKEN_QUERY | TOKEN_DUPLICATE) };
19-
if (!::CreateEnvironmentBlock(&newEnvironmentBlock, processToken.get(), FALSE))
20-
{
21-
return nullptr;
22-
}
23-
return EnvironmentBlockPtr{ newEnvironmentBlock };
24-
}
25-
2612
// Function Description:
2713
// - Updates an EnvironmentVariableMapW with the current process's unicode
2814
// environment variables ignoring ones already set in the provided map.
2915
// Arguments:
3016
// - map: The map to populate with the current processes's environment variables.
31-
// - environmentBlock: Optional environment block to use when filling map. If omitted,
32-
// defaults to the current environment.
3317
// Return Value:
3418
// - S_OK if we succeeded, or an appropriate HRESULT for failing
35-
HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock) noexcept
19+
HRESULT Microsoft::Console::Utils::UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept
3620
try
3721
{
38-
wchar_t const* activeEnvironmentBlock{ static_cast<wchar_t const*>(environmentBlock) };
22+
LPWCH currentEnvVars{};
23+
auto freeCurrentEnv = wil::scope_exit([&] {
24+
if (currentEnvVars)
25+
{
26+
FreeEnvironmentStringsW(currentEnvVars);
27+
currentEnvVars = nullptr;
28+
}
29+
});
3930

40-
wil::unique_environstrings_ptr currentEnvVars;
41-
if (!activeEnvironmentBlock)
42-
{
43-
currentEnvVars.reset(::GetEnvironmentStringsW());
44-
RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars);
45-
activeEnvironmentBlock = currentEnvVars.get();
46-
}
31+
currentEnvVars = ::GetEnvironmentStringsW();
32+
RETURN_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars);
4733

4834
// Each entry is NULL-terminated; block is guaranteed to be double-NULL terminated at a minimum.
49-
for (wchar_t const* lastCh{ activeEnvironmentBlock }; *lastCh != '\0'; ++lastCh)
35+
for (wchar_t const* lastCh{ currentEnvVars }; *lastCh != '\0'; ++lastCh)
5036
{
5137
// Copy current entry into temporary map.
5238
const size_t cchEntry{ ::wcslen(lastCh) };

src/types/inc/Environment.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ namespace Microsoft::Console::Utils
2121
}
2222
};
2323

24-
using EnvironmentBlockPtr = wil::unique_any<void*, decltype(::DestroyEnvironmentBlock), ::DestroyEnvironmentBlock>;
25-
[[nodiscard]] EnvironmentBlockPtr CreateEnvironmentBlock();
26-
2724
using EnvironmentVariableMapW = std::map<std::wstring, std::wstring, WStringCaseInsensitiveCompare>;
2825

29-
[[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map, void* environmentBlock = nullptr) noexcept;
26+
[[nodiscard]] HRESULT UpdateEnvironmentMapW(EnvironmentVariableMapW& map) noexcept;
3027

3128
[[nodiscard]] HRESULT EnvironmentMapToEnvironmentStringsW(EnvironmentVariableMapW& map,
3229
std::vector<wchar_t>& newEnvVars) noexcept;

src/types/precomp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Module Name:
2929

3030
// Windows Header Files:
3131
#include <windows.h>
32-
#include <userenv.h>
3332
#include <combaseapi.h>
3433
#include <UIAutomation.h>
3534
#include <objbase.h>

0 commit comments

Comments
 (0)