# Modifying view index results in silent out-of-bound access

**URL:** <https://discourse.julialang.org/t/modifying-view-index-results-in-silent-out-of-bound-access/63663>\
**Category:** General Usage\
**Tags:** views\
**Created:** [June 27, 2021, 8:16pm UTC](https://discourse.julialang.org/t/modifying-view-index-results-in-silent-out-of-bound-access/63663 "2021-06-27T20:16:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 27, 2021, 8:16pm UTC](https://discourse.julialang.org/t/modifying-view-index-results-in-silent-out-of-bound-access/63663/1 "2021-06-27T20:16:01Z")

</div>

The `view` doc says

> The behavior is undefined if the shape of the parent array is changed after view is called  
> because there is no bound check for the parent array; e.g., it may cause a  
> segmentation fault.

but it doesn’t mention what will happen if I change the index (rather than the parent array).

```julia
julia> ind = [1]
1-element Vector{Int64}:
 1

julia> vals = [10]
1-element Vector{Int64}:
 10

julia> v = view(vals, ind)
1-element view(::Vector{Int64}, [1]) with eltype Int64:
 10

julia> ind[1] = 2
2

julia> v[1]
0

```

What happened here? Is this undefined behavior?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 27, 2021, 8:27pm UTC](https://discourse.julialang.org/t/modifying-view-index-results-in-silent-out-of-bound-access/63663/2 "2021-06-27T20:27:25Z")

</div>

> [@jzr](#):
>
> What happened here? Is this undefined behavior?

It’s undefined behavior. `view` does a bounds check on the indices when the view (`SubArray`) is constructed, but thereafter it assumes that the indices don’t change and hence [it assumes `@inbounds` is correct](https://github.com/JuliaLang/julia/blob/1e3ae2308d5f3ac8fe0c2e47f967e5566edcf0cd/base/subarray.jl#L276) — apparently it reads past the end of the array in this example.

Probably the documentation should have a warning about mutating the indices after a view is constructed. Or possibly the `@inbounds` should be removed here (to be replaced with `@propagate_inbounds` so that the caller can still eliminate the bounds check).
