From cb8a1c43ff96c1d82c8ba1d30e210135303bbe26 Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Mon, 13 Jul 2015 12:57:57 +0200 Subject: Fix Swift crash during start because of spellcheck code Check that checker_ and highlighter_ are initialized before calling them. A crash report indicated a crash calling getMisspelledPositions() on highlighter_ from QtTextEdit::contextMenuEvent(). This fix should prevent this call to cause a crash. Test-Information: Verified that spell checking still works on OS X. Change-Id: Idae37c1f9e2d1a0d0f64da251aa6778f2e9b47a2 diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index fed8819..79a9474 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -152,31 +152,33 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) { replaceWordActions_.clear(); - QAction* insertPoint = menu->actions().first(); - QTextCursor cursor = cursorForPosition(event->pos()); - PositionPair wordPosition = getWordFromCursor(cursor.position()); - if (boost::get<0>(wordPosition) < 0) { - // The click was executed outside a spellable word so no - // suggestions are necessary - return; - } - cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); - cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); - std::vector wordList; - checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList); - if (wordList.size() == 0) { - QAction* noSuggestions = new QAction(tr("No Suggestions"), menu); - noSuggestions->setDisabled(true); - menu->insertAction(insertPoint, noSuggestions); - } - else { - for (std::vector::iterator it = wordList.begin(); it != wordList.end(); ++it) { - QAction* wordAction = new QAction(it->c_str(), menu); - menu->insertAction(insertPoint, wordAction); - replaceWordActions_.push_back(wordAction); + if (checker_ && highlighter_) { + QAction* insertPoint = menu->actions().first(); + QTextCursor cursor = cursorForPosition(event->pos()); + PositionPair wordPosition = getWordFromCursor(cursor.position()); + if (boost::get<0>(wordPosition) < 0) { + // The click was executed outside a spellable word so no + // suggestions are necessary + return; + } + cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); + std::vector wordList; + checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList); + if (wordList.size() == 0) { + QAction* noSuggestions = new QAction(tr("No Suggestions"), menu); + noSuggestions->setDisabled(true); + menu->insertAction(insertPoint, noSuggestions); } + else { + for (std::vector::iterator it = wordList.begin(); it != wordList.end(); ++it) { + QAction* wordAction = new QAction(it->c_str(), menu); + menu->insertAction(insertPoint, wordAction); + replaceWordActions_.push_back(wordAction); + } + } + menu->insertAction(insertPoint, menu->addSeparator()); } - menu->insertAction(insertPoint, menu->addSeparator()); } -- cgit v0.10.2-6-g49f6