Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling init multiple times will leak AtomicPtr in slot with thread panicking. #150

Open
labyrinth-ssr opened this issue Jul 27, 2023 · 1 comment

Comments

@labyrinth-ssr
Copy link

labyrinth-ssr commented Jul 27, 2023

fn init(&self, slot: &Self::Storage, _: c_int) {
let new = Box::default();
let old = slot.0.swap(Box::into_raw(new), Ordering::Release);
// We leak the pointer on purpose here. This is invalid state anyway and must not happen,
// but if it still does, we can't drop that while some other thread might still be having
// the raw pointer.
assert!(old.is_null(), "Init called multiple times");
}

Noticed that if init is called multiple times, the code will panic.

Probable fix is like:
Use compare_and_exchange and only store the value when the slot.0 is null.

@vorner
Copy link
Owner

vorner commented Jul 28, 2023

Hello

Can you explain why you think this is actually a bug? Calling init multiple times is invalid use and the crate doesn't use it that way. So why spend extra effort on supporting it?

I believe that while it would panic, that it still does not lead to UB or anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants