Skip to content

Commit

Permalink
Fix #30006, getindex accessing fields that might not exist (#30405)
Browse files Browse the repository at this point in the history
* Fix #30006, range getindex accessing fields that might not exist
* Add tests for #30006
  • Loading branch information
raghav9-97 authored and ViralBShah committed Dec 17, 2018
1 parent f36ace9 commit 64133f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,8 @@ function getindex(v::AbstractRange{T}, i::Integer) where T
@_inline_meta
ret = convert(T, first(v) + (i - 1)*step_hp(v))
ok = ifelse(step(v) > zero(step(v)),
(ret <= v.stop) & (ret >= v.start),
(ret <= v.start) & (ret >= v.stop))
(ret <= last(v)) & (ret >= first(v)),
(ret <= first(v)) & (ret >= last(v)))
@boundscheck ((i > 0) & ok) || throw_boundserror(v, i)
ret
end
Expand Down
4 changes: 4 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ do33 = fill(1.,3)
end
end

@testset "Issue #30006" begin
SparseMatrixCSC{Float64,Int32}(spzeros(3,3))[:, 1] == [1, 2, 3]
end

@testset "concatenation tests" begin
sp33 = sparse(1.0I, 3, 3)

Expand Down
7 changes: 7 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,13 @@ end
@test getindex((typemax(UInt64)//one(UInt64):typemax(UInt64)//one(UInt64)), 1) == typemax(UInt64)//one(UInt64)
end

@testset "Issue #30006" begin
@test Base.Slice(Base.OneTo(5))[Int32(1)] == Int32(1)
@test Base.Slice(Base.OneTo(3))[Int8(2)] == Int8(2)
@test Base.Slice(1:10)[Int32(2)] == Int32(2)
@test Base.Slice(1:10)[Int8(2)] == Int8(2)
end

@testset "allocation of TwicePrecision call" begin
0:286.493442:360
0:286:360
Expand Down

0 comments on commit 64133f6

Please sign in to comment.