Skip to content

Commit

Permalink
Ensure that unsafe_view doesn't check bounds
Browse files Browse the repository at this point in the history
(cherry picked from commit 32bc971)
ref #20225

and the test from #19555 because it passes, why not
  • Loading branch information
timholy authored and tkelman committed Mar 3, 2017
1 parent a8fbf31 commit fa14563
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function unsafe_view{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
end
function unsafe_view{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
@_inline_meta
idxs = reindex(V, V.indexes, to_indexes(I...))
@inbounds idxs = reindex(V, V.indexes, to_indexes(I...))
SubArray(V.parent, idxs, map(unsafe_length, (index_shape(V.parent, idxs...))))
end

Expand Down
28 changes: 28 additions & 0 deletions test/boundscheck_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,32 @@ end

@test B2() == 0

# issue #19554
function f19554(a)
a[][3]
end
function f19554_2(a, b)
a[][3] = b
return a
end
a19554 = Ref{Array{Float64}}([1 2; 3 4])
@test f19554(a19554) === 2.0
@test f19554_2(a19554, 1) === a19554
@test a19554[][3] === f19554(a19554) === 1.0

# Ensure unsafe_view doesn't check bounds
function V1()
A = rand(10,10)
B = view(A, 4:7, 4:7)
C = Base.unsafe_view(B, -2:7, -2:7)
@test C == A
nothing
end

if bc_opt == bc_default || bc_opt == bc_off
@test V1() == nothing
else
@test_throws BoundsError V1()
end

end

1 comment on commit fa14563

@tkelman
Copy link
Contributor

@tkelman tkelman commented on fa14563 Mar 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, typo, this is actually a backport of #20255

Please sign in to comment.