summaryrefslogtreecommitdiff
path: root/net-p2p/ktorrent/files/ktorrent-5.1.1-scanfolder-memcorruption.patch
blob: 4c41a6af11db7aea860b27aeaac1619c2f8c80b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
From 2d73d5f25a1a364a2115fb1d3262377f7fdf99cd Mon Sep 17 00:00:00 2001
From: Valeriy Malov <jazzvoid@gmail.com>
Date: Wed, 5 Sep 2018 20:43:51 +0300
Subject: Fix memory corruption in ScanFolder plugin

Summary:
Reasoning behind this patch is kinda dubious because I don't fully understand what happens, but it shouldn't make the matters worse either

Seems like deletion of KDirWatch objects from scan_folder in ~ScanThread(), after said QThread has been stopped, causes memory corruption.

Preemptively deleting scan_folder contents before we stop the thread resolves this.

also mark booleans shared across main thread and KDirWatch callbacks as atomic

Test Plan:
Without this patch unloading ScanFolder plugin sometimes crashes ktorrent, including unloading on exit
Doesn't happen anymore with this patch

Reviewers: stikonas

Reviewed By: stikonas

Differential Revision: https://phabricator.kde.org/D15265
---
 plugins/scanfolder/scanthread.cpp | 5 +++++
 plugins/scanfolder/scanthread.h   | 5 +++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/plugins/scanfolder/scanthread.cpp b/plugins/scanfolder/scanthread.cpp
index 0e62768..d7b13e0 100644
--- a/plugins/scanfolder/scanthread.cpp
+++ b/plugins/scanfolder/scanthread.cpp
@@ -156,6 +156,11 @@ namespace kt
     void ScanThread::stop()
     {
         stop_requested = true;
+
+        // XXX seems like deleting KDirWatch object(s) created in scan_folders
+        // in destructor of this QThread after it has been stopped
+        // causes memory corruption, so we delete them early
+        scan_folders.clear();
         exit();
         wait();
     }
diff --git a/plugins/scanfolder/scanthread.h b/plugins/scanfolder/scanthread.h
index d13ca08..fb214a4 100644
--- a/plugins/scanfolder/scanthread.h
+++ b/plugins/scanfolder/scanthread.h
@@ -29,6 +29,7 @@
 #include <util/ptrmap.h>
 #include "scanfolder.h"
 
+#include <atomic>
 
 class QDir;
 
@@ -88,8 +89,8 @@ namespace kt
     private:
         QMutex mutex;
         QStringList folders;
-        bool stop_requested;
-        bool recursive;
+        std::atomic<bool> stop_requested;
+        std::atomic<bool> recursive;
         bt::PtrMap<QString, ScanFolder> scan_folders;
     };
 
-- 
cgit v0.11.2