Repository: microsoft/terminal
File: src/host/input.cpp (C++)

Code before:

    beginMidiSkip();

    // Make our own copy of the console process handle list.
    const auto LimitingProcessId = gci.LimitingProcessId;
    gci.LimitingProcessId = 0;

    std::vector<ConsoleProcessTerminationRecord> termRecords;
    const auto hr = gci.ProcessHandleList.GetTerminationRecordsByGroupId(LimitingProcessId, WI_IsFlagSet(gci.CtrlFlags, CONSOLE_CTRL_CLOSE_FLAG), termRecords);

    if (FAILED(hr) || termRecords.empty())
    {
        gci.UnlockConsole();
        return;
    }

    // Copy ctrl flags.
    const auto CtrlFlags = std::exchange(gci.CtrlFlags, 0);

    gci.UnlockConsole();

    // the ctrl flags could be a combination of the following
    // values:
    //
    //        CONSOLE_CTRL_C_FLAG
    //        CONSOLE_CTRL_BREAK_FLAG
    //        CONSOLE_CTRL_CLOSE_FLAG
    //        CONSOLE_CTRL_LOGOFF_FLAG
    //        CONSOLE_CTRL_SHUTDOWN_FLAG

    auto EventType = (DWORD)-1;
    switch (CtrlFlags & (CONSOLE_CTRL_CLOSE_FLAG | CONSOLE_CTRL_BREAK_FLAG | CONSOLE_CTRL_C_FLAG | CONSOLE_CTRL_LOGOFF_FLAG | CONSOLE_CTRL_SHUTDOWN_FLAG))
    {
    case CONSOLE_CTRL_CLOSE_FLAG:
        EventType = CTRL_CLOSE_EVENT;
        break;

    case CONSOLE_CTRL_BREAK_FLAG:
        EventType = CTRL_BREAK_EVENT;
        break;

    case CONSOLE_CTRL_C_FLAG:
        EventType = CTRL_C_EVENT;
        break;

    case CONSOLE_CTRL_LOGOFF_FLAG:
        EventType = CTRL_LOGOFF_EVENT;
        break;

    case CONSOLE_CTRL_SHUTDOWN_FLAG:
        EventType = CTRL_SHUTDOWN_EVENT;

Review comment (bug):
There's no good reason for this to fail-fast. We can just add a `default:` case to the switch case below, which I now did.

Code after:

    beginMidiSkip();

    // Make our own copy of the console process handle list.
    const auto LimitingProcessId = gci.LimitingProcessId;
    gci.LimitingProcessId = 0;

    std::vector<ConsoleProcessTerminationRecord> termRecords;
    const auto hr = gci.ProcessHandleList
                        .GetTerminationRecordsByGroupId(LimitingProcessId,
                                                        WI_IsFlagSet(gci.CtrlFlags,
                                                                     CONSOLE_CTRL_CLOSE_FLAG),
                                                        termRecords);

    if (FAILED(hr) || termRecords.empty())
    {
        gci.UnlockConsole();
        return;
    }

    // Copy ctrl flags.
    const auto CtrlFlags = std::exchange(gci.CtrlFlags, 0);

    gci.UnlockConsole();

    // the ctrl flags could be a combination of the following
    // values:
    //
    //        CONSOLE_CTRL_C_FLAG
    //        CONSOLE_CTRL_BREAK_FLAG
    //        CONSOLE_CTRL_CLOSE_FLAG
    //        CONSOLE_CTRL_LOGOFF_FLAG
    //        CONSOLE_CTRL_SHUTDOWN_FLAG

    auto EventType = (DWORD)-1;
    switch (CtrlFlags & (CONSOLE_CTRL_CLOSE_FLAG | CONSOLE_CTRL_BREAK_FLAG | CONSOLE_CTRL_C_FLAG | CONSOLE_CTRL_LOGOFF_FLAG | CONSOLE_CTRL_SHUTDOWN_FLAG))
    {
    case CONSOLE_CTRL_CLOSE_FLAG:
        EventType = CTRL_CLOSE_EVENT;
        break;

    case CONSOLE_CTRL_BREAK_FLAG:
        EventType = CTRL_BREAK_EVENT;
        break;

    case CONSOLE_CTRL_C_FLAG:
        EventType = CTRL_C_EVENT;
        break;

    case CONSOLE_CTRL_LOGOFF_FLAG:
        EventType = CTRL_LOGOFF_EVENT;