-
Notifications
You must be signed in to change notification settings - Fork 431
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
ThreadRng: fix use-after-free in TL-dtor; doc for sized iterators #1035
Conversation
Looks good! |
impl ThreadRng { | ||
#[inline(always)] | ||
fn rng(&mut self) -> &mut ReseedingRng<Core, OsRng> { | ||
unsafe { &mut *self.rng.get() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a subtle safety argument here: the caller is required to stop using the return mutable reference before anyone else calls this method. That is crucial to avoid aliasing mutable references.
That might be worth documenting? In particular it might be worth making the function unsafe
as it is not in general safe to use by arbitrary callers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right of course. It's not a public function so I didn't put much thought into its signature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I submitted #1037 to fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RalfJung Which function should be unsafe
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I answered there.)
Fixes #1022. Fixes #968 (except for 0.7 branch).
Revisiting #968, exterior access via the
rng()
function is safe for variant (5). Performance impact is negligible (onnext_u32
andnext_u64
benchmarks). I also checked variant (4) which has a small impact.We could easily add a
safe
feature to opt-out of this unsafe code, but likely that's not sensible.