Skip to content

Commit

Permalink
Make ranges more robust with unsigned indexes. (#50823)
Browse files Browse the repository at this point in the history
Fixes #44895

(cherry picked from commit 91093fe)
  • Loading branch information
gbaraldi authored and IanButterworth committed Aug 25, 2023
1 parent 563a2d3 commit f5e1206
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -944,13 +944,13 @@ end
# This is separate to make it useful even when running with --check-bounds=yes
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
u = i - r.offset
u = oftype(r.offset, i) - r.offset
T(r.ref + u*r.step)
end

function _getindex_hiprec(r::StepRangeLen, i::Integer) # without rounding by T
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
u = i - r.offset
u = oftype(r.offset, i) - r.offset
r.ref + u*r.step
end

Expand Down
4 changes: 2 additions & 2 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ function unsafe_getindex(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, i
# Very similar to _getindex_hiprec, but optimized to avoid a 2nd call to add12
@inline
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
u = i - r.offset
u = oftype(r.offset, i) - r.offset
shift_hi, shift_lo = u*r.step.hi, u*r.step.lo
x_hi, x_lo = add12(r.ref.hi, shift_hi)
T(x_hi + (x_lo + (shift_lo + r.ref.lo)))
end

function _getindex_hiprec(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}, i::Integer)
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
u = i - r.offset
u = oftype(r.offset, i) - r.offset
shift_hi, shift_lo = u*r.step.hi, u*r.step.lo
x_hi, x_lo = add12(r.ref.hi, shift_hi)
x_hi, x_lo = add12(x_hi, x_lo + (shift_lo + r.ref.lo))
Expand Down
7 changes: 7 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2425,3 +2425,10 @@ end
@test collect(r) isa Vector{Int}
@test collect(r) == r
end

@testset "unsigned index #44895" begin
x = range(-1,1,length=11)
@test x[UInt(1)] == -1.0
a = StepRangeLen(1,2,3,2)
@test a[UInt(1)] == -1
end
4 changes: 2 additions & 2 deletions test/testhelpers/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ end
@inline function Base.getindex(r::IdOffsetRange, i::Integer)
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
@boundscheck checkbounds(r, i)
@inbounds eltype(r)(r.parent[i - r.offset] + r.offset)
@inbounds eltype(r)(r.parent[oftype(r.offset, i) - r.offset] + r.offset)
end

# Logical indexing following https://github.com/JuliaLang/julia/pull/31829
Expand Down Expand Up @@ -592,7 +592,7 @@ Base.fill!(A::OffsetArray, x) = parent_call(Ap -> fill!(Ap, x), A)
# Δi = i - first(r)
# i′ = first(r.parent) + Δi
# and one obtains the result below.
parentindex(r::IdOffsetRange, i) = i - r.offset
parentindex(r::IdOffsetRange, i) = oftype(r.offset, i) - r.offset

@propagate_inbounds Base.getindex(A::OffsetArray{<:Any,0}) = A.parent[]

Expand Down

0 comments on commit f5e1206

Please sign in to comment.