From 197ec350e4014330851a013501c6dde3068ac26f Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 20 Dec 2010 17:07:34 +1000 Subject: Setting TextInput.cursorPosition outside bounds crashed. Check bounds in TextInput and TextEdit. Task-number: QTBUG-16188 Reviewed-by: Bea Lam --- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 2 ++ src/declarative/graphicsitems/qdeclarativetextinput.cpp | 2 ++ .../qdeclarativetextedit/tst_qdeclarativetextedit.cpp | 13 +++++++++++++ .../qdeclarativetextinput/tst_qdeclarativetextinput.cpp | 13 +++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index e05f4e4..4e16d24 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -643,6 +643,8 @@ int QDeclarativeTextEdit::cursorPosition() const void QDeclarativeTextEdit::setCursorPosition(int pos) { Q_D(QDeclarativeTextEdit); + if (pos < 0 || pos > d->text.length()) + return; QTextCursor cursor = d->control->textCursor(); if (cursor.position() == pos) return; diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index df103de..521e4ab 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -437,6 +437,8 @@ int QDeclarativeTextInput::cursorPosition() const void QDeclarativeTextInput::setCursorPosition(int cp) { Q_D(QDeclarativeTextInput); + if (cp < 0 || cp > d->control->text().length()) + return; d->control->moveCursor(cp); } diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index a7971cc..c7a51f7 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -652,6 +652,19 @@ void tst_qdeclarativetextedit::selection() QVERIFY(textEditObject->selectionEnd() == 0); QVERIFY(textEditObject->selectedText().isNull()); + // Verify invalid positions are ignored. + textEditObject->setCursorPosition(-1); + QVERIFY(textEditObject->cursorPosition() == 0); + QVERIFY(textEditObject->selectionStart() == 0); + QVERIFY(textEditObject->selectionEnd() == 0); + QVERIFY(textEditObject->selectedText().isNull()); + + textEditObject->setCursorPosition(textEditObject->text().count()+1); + QVERIFY(textEditObject->cursorPosition() == 0); + QVERIFY(textEditObject->selectionStart() == 0); + QVERIFY(textEditObject->selectionEnd() == 0); + QVERIFY(textEditObject->selectedText().isNull()); + //Test selection for(int i=0; i<= testStr.size(); i++) { textEditObject->select(0,i); diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 76e0102..7b2310a 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -354,6 +354,19 @@ void tst_qdeclarativetextinput::selection() QVERIFY(textinputObject->selectionEnd() == 0); QVERIFY(textinputObject->selectedText().isNull()); + // Verify invalid positions are ignored. + textinputObject->setCursorPosition(-1); + QVERIFY(textinputObject->cursorPosition() == 0); + QVERIFY(textinputObject->selectionStart() == 0); + QVERIFY(textinputObject->selectionEnd() == 0); + QVERIFY(textinputObject->selectedText().isNull()); + + textinputObject->setCursorPosition(textinputObject->text().count()+1); + QVERIFY(textinputObject->cursorPosition() == 0); + QVERIFY(textinputObject->selectionStart() == 0); + QVERIFY(textinputObject->selectionEnd() == 0); + QVERIFY(textinputObject->selectedText().isNull()); + //Test selection for(int i=0; i<= testStr.size(); i++) { textinputObject->select(0,i); -- cgit v0.12