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
Fix SB UB (without breaking MSRV) #80
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5dfdcc
Fix UB by not taking a shared reference
clubby789 6c6093d
Re-enable stacked borrows in CI
clubby789 802caca
Use permissive provenance
clubby789 c499836
Use `into_raw` to retrieve the pointer
clubby789 ce5d4df
Remove UB in old approach
clubby789 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
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.
I don't believe this is actually true/safe.
We could use the approach without that optimization, and we could measure it that way to see if there's any actual change in speed. That would be the more elegant way and I suspect it might actually be the case.
If it turns out it is expensive, we could use a different trick:
std::ptr::read
.Arc::into_raw
on that one.Arc::from_raw
.mem::forget
it, so we get rid of it without calling its destructor.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.
That's very true - I must admit I only considered
Rc
and pasted the working code forArc
. I believeshould be safe and work for both variants. Neither
ptr::read
norinto_raw
will affect the refcount (ptr::read
is a bitwise copy andinto_raw
callsmem::forget
to avoid running the destructor).The safety is much easier to verify here - we are calling
ptr::read
on a*const T
derived from an&T
which is guarenteed valid.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.
The previous wasn't safe even for Rc, due to the fact that you could reach the unreachable_unchecked. The first point was true even for Rc.
I know that the
read
is just a bitwise copy andinto_raw
currently calls the forget. In the current version, these are enough. Nevertheless, this is with the knowledge of internal implementation. The internal implementation may change and these properties are not guaranteed. The only description is thatinto_raw
shall be paired withfrom_raw
. Theread
shall then be paired withforget
. So it's about future proofing these for possible change in the standard library.