# Add bound check to get\_unchecked for enabled debug assertions?

**URL:** <https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755>\
**Category:** language design\
**Created:** [June 19, 2018, 4:25pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755 "2018-06-19T16:25:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [June 19, 2018, 4:25pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/1 "2018-06-19T16:25:48Z")

</div>

I don’t know if this idea was proposed previously, but I think it can be useful to add debug assertion which will check bound for `get_unchecked`, `get_unchecked_mut`, `slice_unchecked`, `slice_mut_unchecked` methods (btw shouldn’t we consider deprecation of the last two?) and maybe some other similar methods in the stdlib. It will allow us to easier catch potentially dangerous bugs (e.g. by fuzzing with enabled debug assertions or simply on tests) without hurting performance of release builds. Although one can argue that strictly speaking this change can be considered breaking. What do you think?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 19, 2018, 5:21pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/2 "2018-06-19T17:21:40Z")

</div>

Someone will correct me if I’m wrong, but since indexing past a slice is undefined, I don’t think anyone would object to making it fail sooner & safer in debug mode.

That said, as an example of incorrect code that works:

```rust
let slice1 = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..];
let slice2 = &slice1[..5];
assert_eq!(slice2.get_unchecked(9), 9);

```

This can be “made correct” by casting the slice to the proper length that you know is actually there. This is actually somewhat common in parsing applications: you keep a slice of what this node covers but also an offset to the start of the buffer such that you can look at everything before to find the line/column late instead of eagerly. Since that’s going backwards instead of forwards, though, 100% of them correctly create the slice instead of just indexing past one they already have.

Another wrinkle is that `debug_assert!` in the stdlib basically never runs, as the stdlib is always compiled in optimized mode. This would have to hook into the existing (somewhat hacky workaround) plumbing that enables overflow checks in debug everywhere (or something similar).

However, this could be done exclusively in a crate if you’re ok using different method names: (pseudo-rust for simplicity)

```rust
impl DebugAssertSlicing for &[T] {
    fn get_debug_checked(&self, idx: usize) -> Self::Item {
        if cfg!(debug_assertions) {
            self[idx]
        } else {
            self.get_unchecked(idx)
        }
    }

    // and so on
}

```

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [June 19, 2018, 6:11pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/3 "2018-06-19T18:11:42Z")

</div>

Yeah, `debug_assert!` problem will be the main issue while implementing proposed behaviour.

As for “incorrect code that works” same can be said about integer overflow, so I think it will be a good incentive to write code more “correctly”.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [June 19, 2018, 7:49pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/4 "2018-06-19T19:49:30Z")

</div>

It would be helpful.

Perhaps, as a precaution, there could be another “level” of debug assertions? e.g. `debug_assertions = true` working as it is today, but `debug_assertions = 2` enabling the extra ones.

I can think of more checks for “unchecked” things, e.g. `str::from_utf8_unchecked`, or checks that `&`/`&mut` created from casts in `unsafe` blocks are actually non-NULL.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [June 19, 2018, 7:57pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/5 "2018-06-19T19:57:04Z")

</div>

> [@kornel](#):
>
> another “level” of debug assertions?

This reminds me of sanitizers, in a way. Maybe there could be a "rustsan" that checks these extra things...

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [June 19, 2018, 8:08pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/6 "2018-06-19T20:08:43Z")

</div>

I don’t have a link handy, but I recall this assertion being proposed before and it turned out quite a bit of unsafe code, including in the standard library, is using `get_unchecked_mut` on vectors with indices larger than the length (but smaller than the capacity, and being careful not to _read_ uninitialized memory). Search `src/liballoc/vec.rs` for some examples. While that is somewhat iffy and might technically be UB under the Unsafe Code Guidelines eventually adopted, so far nobody’s put in the work and convincing necessary to change that.

---

<div class="post-metadata">

**Author:** ![gnzlbg](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gnzlbg/32/3858_2.png) [@gnzlbg](https://internals.rust-lang.org/u/gnzlbg)\
**Post date:** [June 19, 2018, 9:28pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/7 "2018-06-19T21:28:28Z")

</div>

The main blockers here are that 1) we never ship `std` with `debug-assertions = true`, and 2) `cargo build` should pick up the `std` library compiled with `debug-assertions = true` for debug builds, and the one with them set to `false` for release builds, and also do all of this depending on the `debug-assertions` value of the profile.

I think we should solve these two issues first, and then adding a whole bunch of `debug_assert`s to `std` might make much more sense. Right now one needs to recompile `std` with `xargo` to benefit from any of these, which is a pretty high barrier of entry in my opinion.

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [June 20, 2018, 12:07am UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/8 "2018-06-20T00:07:53Z")

</div>

I suggested debug assertions on `get_unchecked` and `get_unchecked_mut` back in [this thead](https://internals.rust-lang.org/t/is-std-ops-index-just-a-historical-artefact/6345/8?u=exphp) and was quickly shot down by the fact that… yeah, people do this.

Of note there is bluss’ current solution: [https://crates.io/crates/unchecked-index](https://crates.io/crates/unchecked-index)

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [June 22, 2018, 2:32pm UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/9 "2018-06-22T14:32:11Z")

</div>

I’ve created an [issue](https://github.com/rust-lang/rust/issues/51713) for this proposal.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/add-bound-check-to-get-unchecked-for-enabled-debug-assertions/7755/10 "2019-03-25T08:30:23Z")

</div>

Closed on March 25, 2019, 8:30am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
