From 33f9a7682a9b40d141900aa7ce302a8e47d4d762 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Mon, 15 May 2017 16:27:12 +0200
Subject: Use QPointer to prevent access to potentially freed QtTreeWidget

As the QtTreeWidget and the QtFilterWidget are siblings in
their parent widget, it might happen that the QtTreeWidget
is deleted before the QtFilterWidget. Using Pointer, we are
able to detect this case and can prevent accessing the already
deleted QtTreeWidget.

QtFilterWidget cannot be made the child of the QtTreeWidget in
this case, so this fix uses the QPointers approach as a
workaround.

SWIFT-247

Test-Information:

Tests pass and builds on macOS 10.12.4 with Qt 5.4.2.

Change-Id: I3a60006519b580010718c4d2aa94638555c0afdf

diff --git a/Swift/QtUI/Roster/QtFilterWidget.cpp b/Swift/QtUI/Roster/QtFilterWidget.cpp
index 4df4a39..a52dfff 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.cpp
+++ b/Swift/QtUI/Roster/QtFilterWidget.cpp
@@ -53,6 +53,10 @@ QtFilterWidget::~QtFilterWidget() {
 }
 
 bool QtFilterWidget::eventFilter(QObject*, QEvent* event) {
+    if (!treeView_) {
+        return false;
+    }
+
     if (event->type() == QEvent::KeyPress ||
         event->type() == QEvent::KeyRelease ||
         // InputMethodQuery got introduced in Qt 5.
@@ -119,6 +123,10 @@ void QtFilterWidget::pushAllFilters() {
 }
 
 void QtFilterWidget::updateRosterFilters() {
+    if (!treeView_) {
+        return;
+    }
+
     if (fuzzyRosterFilter_) {
         if (filterLineEdit_->text().isEmpty()) {
             // remove currently installed search filter and put old filters back
@@ -141,6 +149,10 @@ void QtFilterWidget::updateRosterFilters() {
 }
 
 void QtFilterWidget::updateSearchFilter() {
+    if (!treeView_) {
+        return;
+    }
+
     if (fuzzyRosterFilter_) {
         treeView_->getRoster()->removeFilter(fuzzyRosterFilter_);
         delete fuzzyRosterFilter_;
diff --git a/Swift/QtUI/Roster/QtFilterWidget.h b/Swift/QtUI/Roster/QtFilterWidget.h
index ea3c325..85f607e 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.h
+++ b/Swift/QtUI/Roster/QtFilterWidget.h
@@ -5,7 +5,7 @@
  */
 
 /*
- * Copyright (c) 2016 Isode Limited.
+ * Copyright (c) 2016-2017 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -15,6 +15,7 @@
 #include <vector>
 
 #include <QBoxLayout>
+#include <QPointer>
 #include <QWidget>
 
 #include <Swift/Controllers/Roster/FuzzyRosterFilter.h>
@@ -46,7 +47,7 @@ class QtFilterWidget : public QWidget {
 
     private:
         QtClosableLineEdit* filterLineEdit_;
-        QtTreeWidget* treeView_;
+        QPointer<QtTreeWidget> treeView_;
         UIEventStream* eventStream_;
         std::vector<RosterFilter*> filters_;
         QAbstractItemModel* sourceModel_;
-- 
cgit v0.10.2-6-g49f6