Skip to content

Commit

Permalink
Leak pthreax_rwlock_t when it's dropped while locked.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Jun 20, 2022
1 parent d722944 commit e642c59
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/std/src/sys/unix/locks/pthread_rwlock.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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 {
Expand Down

0 comments on commit e642c59

Please sign in to comment.