Skip to content

Add safety requirements for get_unchecked(_mut) in SliceIndex - #165737

Open
yilin0518 wants to merge 1 commit into
rust-lang:mainfrom
yilin0518:SliceIndex_get
Open

yilin0518 wants to merge 1 commit into
rust-lang:mainfrom
yilin0518:SliceIndex_get

Conversation

@yilin0518

@yilin0518 yilin0518 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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)]
fn main() {
    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.

@rustbot rustbot added A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-slice-vec Area: `[T]` and `Vec<T>` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 10, 2026
@rustbot

rustbot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

r? @LawnGnome

rustbot has assigned @LawnGnome.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 38 candidates
  • Random selection from JohnTitor, LawnGnome, Mark-Simulacrum, maxdexh

@maxdexh

maxdexh commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

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.

@yilin0518

Copy link
Copy Markdown
Contributor Author

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-slice-vec Area: `[T]` and `Vec<T>` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants