aboutsummaryrefslogtreecommitdiff
path: root/src/scheduler.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2016-11-18 13:08:30 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2016-11-18 13:10:52 +0100
commit12519bf62b8c49b1c1744eca6ea5b3162a61f962 (patch)
tree71bb2fd2c5b5cb823bb7c0be6098085ce81f3ad8 /src/scheduler.cpp
parenta8b2a82618bec43977a65c076f691ec1cddd54be (diff)
downloadbitcoin-12519bf62b8c49b1c1744eca6ea5b3162a61f962.tar.xz
test: Fix use-after-free in scheduler tests
Make a copy of the boost time-point to wait for, otherwise the head of the queue may be deleted by another thread while this one is waiting, while the boost function still has a reference to it. Although this problem is in non-test code, this is not an actual problem outside of the tests because we use the thread scheduler with only one service thread, so there will never be threads fighting at the head of the queue. The old boost fallback escapes this problem because it passes a scalar value to wait_until instead of a const object reference. Found by running the tests in LLVM-4.0-master asan.
Diffstat (limited to 'src/scheduler.cpp')
-rw-r--r--src/scheduler.cpp7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/scheduler.cpp b/src/scheduler.cpp
index 52777b61f9..27c03f154c 100644
--- a/src/scheduler.cpp
+++ b/src/scheduler.cpp
@@ -54,9 +54,10 @@ void CScheduler::serviceQueue()
#else
// Some boost versions have a conflicting overload of wait_until that returns void.
// Explicitly use a template here to avoid hitting that overload.
- while (!shouldStop() && !taskQueue.empty() &&
- newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) != boost::cv_status::timeout) {
- // Keep waiting until timeout
+ while (!shouldStop() && !taskQueue.empty()) {
+ boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
+ if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout)
+ break; // Exit loop after timeout, it means we reached the time of the event
}
#endif
// If there are multiple threads, the queue can empty while we're waiting (another