aboutsummaryrefslogtreecommitdiff
path: root/src/reverselock.h
diff options
context:
space:
mode:
authorCory Fields <cory-nospam-@coryfields.com>2016-01-05 16:10:13 -0500
committerCory Fields <cory-nospam-@coryfields.com>2016-01-05 17:17:29 -0500
commit89f71c68c0fecf63059f6055ebdd25f1eba4c982 (patch)
tree14c1527701d1c08170678d50757aee2e875ef4ab /src/reverselock.h
parent76ac35f36d87078da62f95b4a1167ec296e37363 (diff)
downloadbitcoin-89f71c68c0fecf63059f6055ebdd25f1eba4c982.tar.xz
c++11: don't throw from the reverselock destructor
noexcept is default for destructors as of c++11. By throwing in reverselock's destructor if it's lock has been tampered with, the likely result is std::terminate being called. Indeed that happened before this change. Once reverselock has taken another lock (its ctor didn't throw), it makes no sense to try to grab or lock the parent lock. That is be broken/undefined behavior depending on the parent lock's implementation, but it shouldn't cause the reverselock to fail to re-lock when destroyed. To avoid those problems, simply swap the parent lock's contents with a dummy for the duration of the lock. That will ensure that any undefined behavior is caught at the call-site rather than the reverse lock's destruction. Barring a failed mutex unlock which would be indicative of a larger problem, the destructor should now never throw.
Diffstat (limited to 'src/reverselock.h')
-rw-r--r--src/reverselock.h5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/reverselock.h b/src/reverselock.h
index 567636e16a..fac1ccb793 100644
--- a/src/reverselock.h
+++ b/src/reverselock.h
@@ -15,10 +15,12 @@ public:
explicit reverse_lock(Lock& lock) : lock(lock) {
lock.unlock();
+ lock.swap(templock);
}
~reverse_lock() {
- lock.lock();
+ templock.lock();
+ templock.swap(lock);
}
private:
@@ -26,6 +28,7 @@ private:
reverse_lock& operator=(reverse_lock const&);
Lock& lock;
+ Lock templock;
};
#endif // BITCOIN_REVERSELOCK_H