From d73f5e65ecbcb6a0acb908b54226edfccf47eccc Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Wed, 25 May 2016 05:44:28 +0100 Subject: Fix undefined behavior when re-locking a mutex from the same thread The only applies to pthread mutexes. We solve this by creating the mutex with the PTHREAD_MUTEX_NORMAL type, which guarantees that re-locking from the same thread will deadlock. --- src/libstd/sync/mutex.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/libstd/sync') diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 90cc79dad66..e53a97eccdf 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -190,10 +190,14 @@ impl Mutex { /// Creates a new mutex in an unlocked state ready for use. #[stable(feature = "rust1", since = "1.0.0")] pub fn new(t: T) -> Mutex { - Mutex { + let mut m = Mutex { inner: box StaticMutex::new(), data: UnsafeCell::new(t), + }; + unsafe { + m.inner.lock.init(); } + m } } -- cgit 1.4.1-3-g733a5