Skip to content

Commit 46b9572

Browse files
authored
Avoid generating SSH profiles using stale memory (#19354)
You can't return a `string_view` to a temporary. It's a miracle this ever worked. Broken since inception in a5f9c85 Closes #19355
1 parent 0aee174 commit 46b9572

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

src/cascadia/TerminalSettingsModel/SshHostGenerator.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,14 @@ using namespace winrt::Microsoft::Terminal::Settings::Model;
3535

3636
/*static*/ const std::wregex SshHostGenerator::_configKeyValueRegex{ LR"(^\s*(\w+)\s+([^\s]+.*[^\s])\s*$)" };
3737

38-
/*static*/ std::wstring_view SshHostGenerator::_getProfileName(const std::wstring_view& hostName) noexcept
38+
winrt::hstring _getProfileName(const std::wstring_view& hostName) noexcept
3939
{
40-
return std::wstring_view{ L"" + PROFILE_TITLE_PREFIX + hostName };
40+
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{0}{1}"), PROFILE_TITLE_PREFIX, hostName) };
4141
}
4242

43-
/*static*/ std::wstring_view SshHostGenerator::_getProfileIconPath() noexcept
43+
winrt::hstring _getProfileCommandLine(const std::wstring_view& sshExePath, const std::wstring_view& hostName) noexcept
4444
{
45-
return PROFILE_ICON_PATH;
46-
}
47-
48-
/*static*/ std::wstring_view SshHostGenerator::_getProfileCommandLine(const std::wstring_view& sshExePath, const std::wstring_view& hostName) noexcept
49-
{
50-
return std::wstring_view{ L"\"" + sshExePath + L"\" " + hostName };
45+
return winrt::hstring{ fmt::format(FMT_COMPILE(LR"("{0}" {1})"), sshExePath, hostName) };
5146
}
5247

5348
/*static*/ bool SshHostGenerator::_tryFindSshExePath(std::wstring& sshExePath) noexcept
@@ -164,8 +159,8 @@ void SshHostGenerator::GenerateProfiles(std::vector<winrt::com_ptr<implementatio
164159
{
165160
const auto profile{ CreateDynamicProfile(_getProfileName(hostName)) };
166161

167-
profile->Commandline(winrt::hstring{ _getProfileCommandLine(sshExePath, hostName) });
168-
profile->Icon(winrt::hstring{ _getProfileIconPath() });
162+
profile->Commandline(_getProfileCommandLine(sshExePath, hostName));
163+
profile->Icon(winrt::hstring{ PROFILE_ICON_PATH });
169164

170165
profiles.emplace_back(profile);
171166
}

src/cascadia/TerminalSettingsModel/SshHostGenerator.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model
3131
private:
3232
static const std::wregex _configKeyValueRegex;
3333

34-
static std::wstring_view _getProfileName(const std::wstring_view& hostName) noexcept;
35-
static std::wstring_view _getProfileIconPath() noexcept;
36-
static std::wstring_view _getProfileCommandLine(const std::wstring_view& sshExePath, const std::wstring_view& hostName) noexcept;
37-
3834
static bool _tryFindSshExePath(std::wstring& sshExePath) noexcept;
3935
static bool _tryParseConfigKeyValue(const std::wstring_view& line, std::wstring& key, std::wstring& value) noexcept;
4036
static void _getHostNamesFromConfigFile(const std::wstring_view& configPath, std::vector<std::wstring>& hostNames) noexcept;

0 commit comments

Comments
 (0)