Fix a crash when there aren't any recentCommands yet (#11082)

The first time you open commandline mode, `recentCommands` doesn't exist yet. However, we immediately try to read the `Size()` in a couple places. This'll A/V and we'll crash 😨 

The fix is easy - don't try and read the size of the non-existent `recentCommands`

Found this while playing with #11069
Regressed in #11030 
Didn't bother filing an issue for it when I have the fix in hand
This commit is contained in:
Mike Griese 2021-08-31 06:07:30 -05:00 committed by GitHub
parent efea1e5bad
commit 717ea85c9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1241,6 +1241,13 @@ namespace winrt::TerminalApp::implementation
IVector<TerminalApp::FilteredCommand> CommandPalette::_loadRecentCommands()
{
const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
// If this is the first time we've opened the commandline mode and
// there aren't any recent commands, then just return an empty vector.
if (!recentCommands)
{
return single_threaded_vector<TerminalApp::FilteredCommand>();
}
std::vector<TerminalApp::FilteredCommand> parsedCommands;
parsedCommands.reserve(std::min(recentCommands.Size(), CommandLineHistoryLength));
@ -1268,7 +1275,9 @@ namespace winrt::TerminalApp::implementation
void CommandPalette::_updateRecentCommands(const hstring& command)
{
const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
const auto countToCopy = std::min(recentCommands.Size(), CommandLineHistoryLength - 1);
// If there aren't and recent commands already in the state, then we
// don't need to copy any.
const auto countToCopy = std::min(recentCommands ? recentCommands.Size() : 0, CommandLineHistoryLength - 1);
std::vector<hstring> newRecentCommands{ countToCopy + 1 };
til::at(newRecentCommands, 0) = command;
if (countToCopy)