Fix crash in QAccessibleTextWidget::attributes()

Task-number: QTBUG-44006
Change-Id: I79d7d84206a3e4abcd49c7c6e5e91b7c9c753dd6
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
This commit is contained in:
Jan Arve Saether 2015-02-16 17:17:23 +01:00 committed by Jan Arve Sæther
parent bcfacd4b6f
commit 395f0181ee
2 changed files with 21 additions and 8 deletions

View File

@ -727,19 +727,31 @@ QString QAccessibleTextWidget::attributes(int offset, int *startOffset, int *end
int blockEnd = blockStart + block.length(); int blockEnd = blockStart + block.length();
QTextBlock::iterator iter = block.begin(); QTextBlock::iterator iter = block.begin();
while (!iter.fragment().contains(offset)) int lastFragmentIndex = blockStart;
while (!iter.atEnd()) {
QTextFragment f = iter.fragment();
if (f.contains(offset))
break;
lastFragmentIndex = f.position() + f.length();
++iter; ++iter;
}
QTextFragment fragment = iter.fragment(); QTextCharFormat charFormat;
int pos = fragment.position(); if (!iter.atEnd()) {
QTextFragment fragment = iter.fragment();
// text block and fragment may overlap, use the smallest common range charFormat = fragment.charFormat();
*startOffset = qMax(pos, blockStart); int pos = fragment.position();
// text block and fragment may overlap, use the smallest common range
*startOffset = qMax(pos, blockStart);
*endOffset = qMin(pos + fragment.length(), blockEnd);
} else {
charFormat = block.charFormat();
*startOffset = lastFragmentIndex;
*endOffset = blockEnd;
}
Q_ASSERT(*startOffset <= offset); Q_ASSERT(*startOffset <= offset);
*endOffset = qMin(pos + fragment.length(), blockEnd);
Q_ASSERT(*endOffset >= offset); Q_ASSERT(*endOffset >= offset);
QTextCharFormat charFormat = fragment.charFormat();
QTextBlockFormat blockFormat = cursor.blockFormat(); QTextBlockFormat blockFormat = cursor.blockFormat();
QMap<QByteArray, QString> attrs; QMap<QByteArray, QString> attrs;

View File

@ -733,6 +733,7 @@ void tst_QAccessibility::textAttributes_data()
QTest::newRow("left right left aligned 10") << leftRightLeftAligned << 10 << 10 << 11 << rightAlign; QTest::newRow("left right left aligned 10") << leftRightLeftAligned << 10 << 10 << 11 << rightAlign;
QTest::newRow("left right left aligned 11") << leftRightLeftAligned << 11 << 11 << 15 << bold; QTest::newRow("left right left aligned 11") << leftRightLeftAligned << 11 << 11 << 15 << bold;
QTest::newRow("left right left aligned 15") << leftRightLeftAligned << 15 << 11 << 15 << bold; QTest::newRow("left right left aligned 15") << leftRightLeftAligned << 15 << 11 << 15 << bold;
QTest::newRow("empty with no fragments") << QString::fromLatin1("\n\n\n\n") << 0 << 0 << 1 << defaults;
} }
void tst_QAccessibility::textAttributes() void tst_QAccessibility::textAttributes()