about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2022-06-19 12:53:12 +0200
committerMara Bos <m-ou.se@m-ou.se>2022-06-20 09:33:59 +0200
commite642c5987e1885a6ea9b0f1527810a72bdcdeb3f (patch)
treea3ad3b7d6c42578b8d02784911c1c57533347495
parentd72294491c5d28449c49b884b620cc24b3cf010f (diff)
downloadrust-e642c5987e1885a6ea9b0f1527810a72bdcdeb3f.tar.gz
rust-e642c5987e1885a6ea9b0f1527810a72bdcdeb3f.zip
Leak pthreax_rwlock_t when it's dropped while locked.
-rw-r--r--library/std/src/sys/unix/locks/pthread_rwlock.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/locks/pthread_rwlock.rs b/library/std/src/sys/unix/locks/pthread_rwlock.rs
index 75e5759c787..adfe2a88338 100644
--- a/library/std/src/sys/unix/locks/pthread_rwlock.rs
+++ b/library/std/src/sys/unix/locks/pthread_rwlock.rs
@@ -1,4 +1,5 @@
 use crate::cell::UnsafeCell;
+use crate::mem::forget;
 use crate::sync::atomic::{AtomicUsize, Ordering};
 use crate::sys_common::lazy_box::{LazyBox, LazyInit};
 
@@ -17,6 +18,21 @@ impl LazyInit for RwLock {
     fn init() -> Box<Self> {
         Box::new(Self::new())
     }
+
+    fn destroy(mut rwlock: Box<Self>) {
+        // We're not allowed to pthread_rwlock_destroy a locked rwlock,
+        // so check first if it's unlocked.
+        if *rwlock.write_locked.get_mut() || *rwlock.num_readers.get_mut() != 0 {
+            // The rwlock is locked. This happens if a RwLock{Read,Write}Guard is leaked.
+            // In this case, we just leak the RwLock too.
+            forget(rwlock);
+        }
+    }
+
+    fn cancel_init(_: Box<Self>) {
+        // In this case, we can just drop it without any checks,
+        // since it cannot have been locked yet.
+    }
 }
 
 impl RwLock {