From e171d1e8c4b4324374cb57a6d7d181a57fc17dbe Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Fri, 31 May 2013 12:13:40 +1000 Subject: [PATCH] Fix assert when calculating the implicit width of truncated lines. Guard against reading past the end of the final line. Task-number: QTBUG-31471 Change-Id: I489f742936ee16f12ad9762b7c0891bfa9377e21 Reviewed-by: Martin Jones --- src/quick/items/qquicktext.cpp | 5 ++++- .../quick/qquicktext/data/elideBeforeMaximumLineCount.qml | 10 ++++++++++ tests/auto/quick/qquicktext/tst_qquicktext.cpp | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/auto/quick/qquicktext/data/elideBeforeMaximumLineCount.qml diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp index cf9cb42..fbf46f5 100644 --- a/src/quick/items/qquicktext.cpp +++ b/src/quick/items/qquicktext.cpp @@ -954,7 +954,10 @@ QRectF QQuickTextPrivate::setupTextLayout(qreal *const baseline) // Create the remainder of the unwrapped lines up to maxLineCount to get the // implicit width. - if (line.isValid() && layoutText.at(line.textStart() + line.textLength()) != QChar::LineSeparator) + const int eol = line.isValid() + ? line.textStart() + line.textLength() + : layoutText.length(); + if (eol < layoutText.length() && layoutText.at(eol) != QChar::LineSeparator) line = layout.createLine(); for (; line.isValid() && unwrappedLineCount <= maxLineCount; ++unwrappedLineCount) line = layout.createLine(); diff --git a/tests/auto/quick/qquicktext/data/elideBeforeMaximumLineCount.qml b/tests/auto/quick/qquicktext/data/elideBeforeMaximumLineCount.qml new file mode 100644 index 0000000..7f1ce70 --- /dev/null +++ b/tests/auto/quick/qquicktext/data/elideBeforeMaximumLineCount.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 + +Text { + width: implicitWidth / 2 + height: implicitHeight / 2 + elide: Text.ElideRight + maximumLineCount: 4 + + text: "Line one\nLine two\nLine three\nLine four" +} diff --git a/tests/auto/quick/qquicktext/tst_qquicktext.cpp b/tests/auto/quick/qquicktext/tst_qquicktext.cpp index b95a646..fb3b62b 100644 --- a/tests/auto/quick/qquicktext/tst_qquicktext.cpp +++ b/tests/auto/quick/qquicktext/tst_qquicktext.cpp @@ -149,6 +149,8 @@ private slots: void htmlLists(); void htmlLists_data(); + void elideBeforeMaximumLineCount(); + private: QStringList standard; QStringList richText; @@ -3638,6 +3640,18 @@ void tst_qquicktext::htmlLists_data() QTest::newRow("unordered list bad") << "" << 2; } +void tst_qquicktext::elideBeforeMaximumLineCount() +{ // QTBUG-31471 + QQmlComponent component(&engine, testFile("elideBeforeMaximumLineCount.qml")); + + QScopedPointer object(component.create()); + + QQuickText *item = qobject_cast(object.data()); + QVERIFY(item); + + QCOMPARE(item->lineCount(), 2); +} + QTEST_MAIN(tst_qquicktext) #include "tst_qquicktext.moc" -- 2.7.4