Skip to content

Commit 4858e8c

Browse files
gbaraldinalimilan
authored andcommitted
Make ranges more robust with unsigned indexes. (#50823)
Fixes #44895 (cherry picked from commit 91093fe)
1 parent e2c5dfb commit 4858e8c

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

base/range.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,13 @@ end
959959
# This is separate to make it useful even when running with --check-bounds=yes
960960
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T
961961
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
962-
u = i - r.offset
962+
u = oftype(r.offset, i) - r.offset
963963
T(r.ref + u*r.step)
964964
end
965965

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

base/twiceprecision.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,15 +478,15 @@ function unsafe_getindex(r::StepRangeLen{T,<:TwicePrecision,<:TwicePrecision}, i
478478
# Very similar to _getindex_hiprec, but optimized to avoid a 2nd call to add12
479479
@inline
480480
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
481-
u = i - r.offset
481+
u = oftype(r.offset, i) - r.offset
482482
shift_hi, shift_lo = u*r.step.hi, u*r.step.lo
483483
x_hi, x_lo = add12(r.ref.hi, shift_hi)
484484
T(x_hi + (x_lo + (shift_lo + r.ref.lo)))
485485
end
486486

487487
function _getindex_hiprec(r::StepRangeLen{<:Any,<:TwicePrecision,<:TwicePrecision}, i::Integer)
488488
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
489-
u = i - r.offset
489+
u = oftype(r.offset, i) - r.offset
490490
shift_hi, shift_lo = u*r.step.hi, u*r.step.lo
491491
x_hi, x_lo = add12(r.ref.hi, shift_hi)
492492
x_hi, x_lo = add12(x_hi, x_lo + (shift_lo + r.ref.lo))

test/ranges.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,3 +2510,10 @@ end
25102510
@test collect(r) isa Vector{Int}
25112511
@test collect(r) == r
25122512
end
2513+
2514+
@testset "unsigned index #44895" begin
2515+
x = range(-1,1,length=11)
2516+
@test x[UInt(1)] == -1.0
2517+
a = StepRangeLen(1,2,3,2)
2518+
@test a[UInt(1)] == -1
2519+
end

test/testhelpers/OffsetArrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ end
142142
@inline function Base.getindex(r::IdOffsetRange, i::Integer)
143143
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
144144
@boundscheck checkbounds(r, i)
145-
@inbounds eltype(r)(r.parent[i - r.offset] + r.offset)
145+
@inbounds eltype(r)(r.parent[oftype(r.offset, i) - r.offset] + r.offset)
146146
end
147147

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

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

0 commit comments

Comments
 (0)