Skip to content

Commit e642c59

Browse files
committed
Leak pthreax_rwlock_t when it's dropped while locked.
1 parent d722944 commit e642c59

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/std/src/sys/unix/locks/pthread_rwlock.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::cell::UnsafeCell;
2+
use crate::mem::forget;
23
use crate::sync::atomic::{AtomicUsize, Ordering};
34
use crate::sys_common::lazy_box::{LazyBox, LazyInit};
45

@@ -17,6 +18,21 @@ impl LazyInit for RwLock {
1718
fn init() -> Box<Self> {
1819
Box::new(Self::new())
1920
}
21+
22+
fn destroy(mut rwlock: Box<Self>) {
23+
// We're not allowed to pthread_rwlock_destroy a locked rwlock,
24+
// so check first if it's unlocked.
25+
if *rwlock.write_locked.get_mut() || *rwlock.num_readers.get_mut() != 0 {
26+
// The rwlock is locked. This happens if a RwLock{Read,Write}Guard is leaked.
27+
// In this case, we just leak the RwLock too.
28+
forget(rwlock);
29+
}
30+
}
31+
32+
fn cancel_init(_: Box<Self>) {
33+
// In this case, we can just drop it without any checks,
34+
// since it cannot have been locked yet.
35+
}
2036
}
2137

2238
impl RwLock {

0 commit comments

Comments
 (0)