Skip to content

Commit 7352037

Browse files
raghav9-97KristofferC
authored andcommitted
Fix #30006, getindex accessing fields that might not exist (#30405)
* Fix #30006, range getindex accessing fields that might not exist * Add tests for #30006 (cherry picked from commit 64133f6)
1 parent aede024 commit 7352037

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

base/range.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ function getindex(v::AbstractRange{T}, i::Integer) where T
608608
@_inline_meta
609609
ret = convert(T, first(v) + (i - 1)*step_hp(v))
610610
ok = ifelse(step(v) > zero(step(v)),
611-
(ret <= v.stop) & (ret >= v.start),
612-
(ret <= v.start) & (ret >= v.stop))
611+
(ret <= last(v)) & (ret >= first(v)),
612+
(ret <= first(v)) & (ret >= last(v)))
613613
@boundscheck ((i > 0) & ok) || throw_boundserror(v, i)
614614
ret
615615
end

stdlib/SparseArrays/test/sparse.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ do33 = fill(1.,3)
9292
end
9393
end
9494

95+
@testset "Issue #30006" begin
96+
SparseMatrixCSC{Float64,Int32}(spzeros(3,3))[:, 1] == [1, 2, 3]
97+
end
98+
9599
@testset "concatenation tests" begin
96100
sp33 = sparse(1.0I, 3, 3)
97101

test/ranges.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,17 @@ end
14061406
@test @inferred(z4 .+ z4) === z4
14071407
end
14081408

1409+
@testset "getindex" begin
1410+
@test getindex((typemax(UInt64)//one(UInt64):typemax(UInt64)//one(UInt64)), 1) == typemax(UInt64)//one(UInt64)
1411+
end
1412+
1413+
@testset "Issue #30006" begin
1414+
@test Base.Slice(Base.OneTo(5))[Int32(1)] == Int32(1)
1415+
@test Base.Slice(Base.OneTo(3))[Int8(2)] == Int8(2)
1416+
@test Base.Slice(1:10)[Int32(2)] == Int32(2)
1417+
@test Base.Slice(1:10)[Int8(2)] == Int8(2)
1418+
end
1419+
14091420
@testset "allocation of TwicePrecision call" begin
14101421
0:286.493442:360
14111422
0:286:360

0 commit comments

Comments
 (0)