summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Funk <kfunk@kde.org>2014-08-28 15:46:03 +0200
committerKevin Funk <kevin.funk@kdab.com>2014-09-02 14:18:08 +0200
commiteb447679456336d387bb69a56c164b06fbe83166 (patch)
tree6bc3edc6700f126d751a4dedb77cc5b0b9d30a73
parentb9a7cedb6ebd85f249f54c8688943346c7a98260 (diff)
Fix crash in QTextLayout::cursorToX
When 'cursorPos' is out of bounds ([0, lineEnd]), this method crashed. Change-Id: Ia0540ab3afbffb5c598f7b8515263cce3b3928e4 Task-number: QTBUG-40753 Reviewed-by: Dominik Haumann <dhaumann@kde.org> Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com> Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
-rw-r--r--src/gui/text/qtextlayout.cpp2
-rw-r--r--tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp23
2 files changed, 24 insertions, 1 deletions
diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp
index 4879ae51d7..8e605e4ae1 100644
--- a/src/gui/text/qtextlayout.cpp
+++ b/src/gui/text/qtextlayout.cpp
@@ -2583,7 +2583,7 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const
}
int lineEnd = line.from + line.length + line.trailingSpaces;
- int pos = *cursorPos;
+ int pos = qBound(0, *cursorPos, lineEnd);
int itm;
const QCharAttributes *attributes = eng->attributes();
if (!attributes) {
diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
index ec698e5db4..e49d8c3b07 100644
--- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
+++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
@@ -86,6 +86,7 @@ private slots:
void cursorToXForSetColumns();
void cursorToXForTrailingSpaces_data();
void cursorToXForTrailingSpaces();
+ void cursorToXInvalidInput();
void horizontalAlignment_data();
void horizontalAlignment();
void horizontalAlignmentMultiline_data();
@@ -674,6 +675,28 @@ void tst_QTextLayout::cursorToXForTrailingSpaces()
QCOMPARE(line.cursorToX(6), cursorAt6);
}
+void tst_QTextLayout::cursorToXInvalidInput()
+{
+ QTextLayout layout("aaa", testFont);
+
+ layout.beginLayout();
+ QTextLine line = layout.createLine();
+ line.setLineWidth(5);
+ layout.endLayout();
+
+ int cursorPos;
+
+ cursorPos = 0;
+ layout.lineAt(0).cursorToX(&cursorPos);
+ QCOMPARE(cursorPos, 0);
+ cursorPos = -300;
+ layout.lineAt(0).cursorToX(&cursorPos);
+ QCOMPARE(cursorPos, 0);
+ cursorPos = 300;
+ layout.lineAt(0).cursorToX(&cursorPos);
+ QCOMPARE(cursorPos, 3);
+}
+
void tst_QTextLayout::horizontalAlignment_data()
{
qreal width = TESTFONT_SIZE * 4;