Repository: microsoft/terminal
File: src/types/UiaTextRangeBase.cpp (C++)

Code before:
    {
        _expandToEnclosingUnit(unit);
        UiaTracing::TextRange::ExpandToEnclosingUnit(unit, *this);
        return S_OK;
    }
    CATCH_RETURN();
}

// Method Description:
// - Moves _start and _end endpoints to encompass the enclosing text unit.
//   (i.e. word --> enclosing word, line --> enclosing line)
// - IMPORTANT: this does _not_ lock the console
// Arguments:
// - attributeId - the UIA text attribute identifier we're expanding by
// Return Value:
// - <none>
void UiaTextRangeBase::_expandToEnclosingUnit(TextUnit unit)
{
    const auto& buffer = _pData->GetTextBuffer();
    const auto bufferSize{ buffer.GetSize() };
    const auto documentEnd{ _getDocumentEnd() };

    // If we're past document end,
    // set us to ONE BEFORE the document end.
    // This allows us to expand properly.
    if (bufferSize.CompareInBounds(_start, documentEnd, true) >= 0)
    {
        _start = documentEnd;
        bufferSize.DecrementInBounds(_start, true);
    }

    if (unit == TextUnit_Character)
    {
        _start = buffer.GetGlyphStart(_start, documentEnd);
        _end = buffer.GetGlyphEnd(_start, true, documentEnd);
    }
    else if (unit <= TextUnit_Word)
    {
        // expand to word
        _start = buffer.GetWordStart(_start, _wordDelimiters, true, documentEnd);
        _end = buffer.GetWordEnd(_start, _wordDelimiters, true, documentEnd);
    }
    else if (unit <= TextUnit_Line)
    {
        // expand to line
        _start.x = 0;
        if (_start.y == documentEnd.y)
        {
            // we're on the last line
            _end = documentEnd;
            bufferSize.IncrementInBounds(_end, true);

Review comment (bug):
FYI: The final boolean parameter in `CompareInBounds` is ignored in Release mode builds anyways, so from a perspective of "could this potentially crash?" it doesn't matter if the old code used to have `true`, `false`, or no boolean argument at all.

Code after:
{
    const auto& buffer = _pData->GetTextBuffer();
    const auto bufferSize{ buffer.GetSize() };
    const auto documentEnd{ _getDocumentEnd() };

    // If we're past document end,
    // set us to ONE BEFORE the document end.
    // This allows us to expand properly.
    if (_start >= documentEnd)
    {
        _start = documentEnd;
        bufferSize.DecrementInBounds(_start, true);
    }

    if (unit == TextUnit_Character)
    {
        _start = buffer.GetGlyphStart(_start, documentEnd);
        _end = buffer.GetGlyphEnd(_start, true, documentEnd);
    }
    else if (unit <= TextUnit_Word)
    {
        // expand to word
        _start = buffer.GetWordStart(_start, _wordDelimiters, true, documentEnd);
        _end = buffer.GetWordEnd(_start, _wordDelimiters, true, documentEnd);
    }
    else if (unit <= TextUnit_Line)
    {
        // expand to line
        _start.x = 0;
        if (_start.y == documentEnd.y)
        {
            // we're on the last line
            _end = documentEnd;
            bufferSize.IncrementInBounds(_end, true);
        }
        else
        {
            _end.x = 0;
            _end.y = _start.y + 1;
        }
    }
    else
    {
        // expand to document
        _start = bufferSize.Origin();
        _end = documentEnd;
    }
}

// Method Description:
// - Verify that the given attribute has the desired formatting saved in the attributeId and val