-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Don't use NonNull::dangling as sentinel value in Rc, Arc #52637
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,6 +258,7 @@ use core::ops::Deref; | |
use core::ops::CoerceUnsized; | ||
use core::ptr::{self, NonNull}; | ||
use core::convert::From; | ||
use core::usize; | ||
|
||
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error}; | ||
use string::String; | ||
|
@@ -449,6 +450,8 @@ impl<T: ?Sized> Rc<T> { | |
#[stable(feature = "rc_weak", since = "1.4.0")] | ||
pub fn downgrade(this: &Self) -> Weak<T> { | ||
this.inc_weak(); | ||
// Make sure we do not create a dangling Weak | ||
debug_assert!(!is_dangling(this.ptr)); | ||
Weak { ptr: this.ptr } | ||
} | ||
|
||
|
@@ -1154,8 +1157,9 @@ impl<T> From<Vec<T>> for Rc<[T]> { | |
pub struct Weak<T: ?Sized> { | ||
// This is a `NonNull` to allow optimizing the size of this type in enums, | ||
// but it is not necessarily a valid pointer. | ||
// `Weak::new` sets this to a dangling pointer so that it doesn’t need | ||
// to allocate space on the heap. | ||
// `Weak::new` sets this to `usize::MAX` so that it doesn’t need | ||
// to allocate space on the heap. That's not a value a real pointer | ||
// will ever have because RcBox has alignment at least 2. | ||
ptr: NonNull<RcBox<T>>, | ||
} | ||
|
||
|
@@ -1185,15 +1189,14 @@ impl<T> Weak<T> { | |
#[stable(feature = "downgraded_weak", since = "1.10.0")] | ||
pub fn new() -> Weak<T> { | ||
Weak { | ||
ptr: NonNull::dangling(), | ||
ptr: NonNull::new(usize::MAX as *mut RcBox<T>).expect("MAX is not 0"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better than unsafe code, I figured. :P |
||
} | ||
} | ||
} | ||
|
||
pub(crate) fn is_dangling<T: ?Sized>(ptr: NonNull<T>) -> bool { | ||
let address = ptr.as_ptr() as *mut () as usize; | ||
let align = align_of_val(unsafe { ptr.as_ref() }); | ||
address == align | ||
address == usize::MAX | ||
} | ||
|
||
impl<T: ?Sized> Weak<T> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this assert is not gonna show up in libstd for users. Libstd is compiled in release mode
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.
We have tests using the debug profile in the CI, so keeping this is fine.
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.
Yeah, I didn't want to risk an actual perf hit (though it should get all optimized out, because LLVM knows the pointer to be aligned).