You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add safety requirements for get_unchecked(_mut) in SliceIndex - #165737
This PR tries to fix the issue #155312, which talks about that get_unchecked(_mut) methods in SliceIndex have soundness issue. I use the following code to check this problem:
#![feature(slice_ptr_get)]fnmain(){let data = [10_i32,20,30,40];let slice:*const[i32] = &data[..];let range = 3..=1;let result = unsafe{ slice.get_unchecked(range)};println!("{result:p}");}
In nightly verion, use cargo run will get the following output:
thread 'main' (38472) panicked at C:\Users\14798\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library/core\src\slice\index.rs:668:42:
unsafe precondition(s) violated: slice::get_unchecked requires that the range is within the slice
This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting.
error: process didn't exit successfully: `target\debug\day1_tcp_echo.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)
The problem seems that the safety doc of get_unchecked(_mut) is not enough. The range can trigger UB without out-of-bounds, the start is bigger than the end index is enough. So in this PR I add this requirement for SliceIndex and core::ptr modules.
I can update this PR if there is better solution. Thank you for your review and suggestion.
I don't think this is the right approach. The safety requirements of get_unchecked depend on the implementing type and thus should be either declared on the impls (with the trait method's safety requirements deferring to them), or be phrased in terms of other methods of the trait.
@maxdexh you are right. Yeaterday we talked about PR#165681, and at that time I didn't come up with a great idea about how to improve this PR. I think be phrased in terms of other methods of the trait. is better than declared on the impls, because the latter will cause big maintenance cost. So I refer to get(_mut) in SliceIndex and [T]::get_disjoint_unchecked_mut. But it seems a little redundant.
Besides, the changes in core::ptr remain unchanged, because I haven't thought of great ideas about updating these. Pointer doe not have corresponding get(_mut) methods.
I'm expecting your suggestion. Thank you.
This branch has not been deployed
No deployments
This file contains hidden or 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
A-raw-pointersArea: raw pointers, MaybeUninit, NonNullA-slice-vecArea: `[T]` and `Vec<T>`S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties.T-libsRelevant to the library team, which will review and decide on the PR/issue.
4 participants
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.
This PR tries to fix the issue #155312, which talks about that
get_unchecked(_mut)methods inSliceIndexhave soundness issue. I use the following code to check this problem:In nightly verion, use
cargo runwill get the following output:The problem seems that the safety doc of
get_unchecked(_mut)is not enough. The range can trigger UB without out-of-bounds, the start is bigger than the end index is enough. So in this PR I add this requirement forSliceIndexandcore::ptrmodules.I can update this PR if there is better solution. Thank you for your review and suggestion.