Skip to content

Commit 7f4efb8

Browse files
authored
Merge pull request #1 from shanselman/copilot/review-new-changes
Fix unhandled GUID parsing exceptions in --session options
2 parents 77934eb + 2618cd7 commit 7f4efb8

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/cascadia/LocalTests_TerminalApp/CommandlineTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,16 @@ namespace TerminalAppLocalTests
11801180
VERIFY_ARE_NOT_EQUAL("", appArgs._exitMessage);
11811181
}
11821182
}
1183+
{
1184+
Log::Comment(NoThrowString().Format(
1185+
L"Test that --session with an invalid GUID doesn't crash"));
1186+
AppCommandlineArgs appArgs{};
1187+
std::vector<const wchar_t*> rawCommands{ L"wt.exe", subcommand, L"-s", L"invalid-guid-string" };
1188+
_buildCommandlinesHelper(appArgs, 1u, rawCommands);
1189+
1190+
// With an invalid GUID, the command should be silently skipped (no action created)
1191+
VERIFY_ARE_EQUAL(0u, appArgs._startupActions.size());
1192+
}
11831193
}
11841194

11851195
void CommandlineTest::ParseMoveFocusArgs()

src/cascadia/TerminalApp/AppCommandlineArgs.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -393,31 +393,45 @@ void AppCommandlineArgs::_buildFocusTabParser()
393393
if (!_focusTabSession.empty())
394394
{
395395
// Focus tab by session GUID
396-
focusTabAction.Action(ShortcutAction::SwitchToTab);
397-
SwitchToTabArgs args{};
398396
const auto str = winrt::to_hstring(_focusTabSession);
399397
// GuidFromPlainString handles GUIDs without braces (e.g. "12345678-...")
400398
// GuidFromString handles GUIDs with braces (e.g. "{12345678-...}")
401399
// Try plain first (most common from WT_SESSION), fall back to braced
402400
winrt::guid id{};
401+
bool parsedSuccessfully = false;
403402
try
404403
{
405-
id = ::Microsoft::Console::Utils::GuidFromPlainString(str.c_str());
404+
try
405+
{
406+
id = ::Microsoft::Console::Utils::GuidFromPlainString(str.c_str());
407+
}
408+
catch (...)
409+
{
410+
id = ::Microsoft::Console::Utils::GuidFromString(str.c_str());
411+
}
412+
parsedSuccessfully = true;
406413
}
407414
catch (...)
408415
{
409-
id = ::Microsoft::Console::Utils::GuidFromString(str.c_str());
416+
// Invalid GUID provided - silently ignore similar to how invalid
417+
// tab colors are handled. The command will be skipped.
410418
}
411-
args.SessionId(id);
412-
focusTabAction.Args(args);
413-
_startupActions.push_back(focusTabAction);
414-
415-
// When targeting by session ID, route to an existing window on the
416-
// current desktop (if user didn't specify -w explicitly).
417-
// The session ID uniquely identifies a pane in some window.
418-
if (_windowTarget.empty())
419+
420+
if (parsedSuccessfully)
419421
{
420-
_windowTarget = "0";
422+
focusTabAction.Action(ShortcutAction::SwitchToTab);
423+
SwitchToTabArgs args{};
424+
args.SessionId(id);
425+
focusTabAction.Args(args);
426+
_startupActions.push_back(focusTabAction);
427+
428+
// When targeting by session ID, route to an existing window on the
429+
// current desktop (if user didn't specify -w explicitly).
430+
// The session ID uniquely identifies a pane in some window.
431+
if (_windowTarget.empty())
432+
{
433+
_windowTarget = "0";
434+
}
421435
}
422436
}
423437
else if (_focusTabIndex >= 0)
@@ -736,8 +750,16 @@ NewTerminalArgs AppCommandlineArgs::_getNewTerminalArgs(AppCommandlineArgs::NewT
736750
if (*subcommand.sessionIdOption)
737751
{
738752
const auto str = winrt::to_hstring(_sessionId);
739-
const auto id = ::Microsoft::Console::Utils::GuidFromString(str.c_str());
740-
args.SessionId(id);
753+
try
754+
{
755+
const auto id = ::Microsoft::Console::Utils::GuidFromString(str.c_str());
756+
args.SessionId(id);
757+
}
758+
catch (...)
759+
{
760+
// Invalid GUID provided - silently ignore similar to how invalid
761+
// tab colors are handled.
762+
}
741763
}
742764

743765
if (*subcommand.startingDirectoryOption)

0 commit comments

Comments
 (0)