Skip to content

Commit 91093fe

Browse files
authored
Make ranges more robust with unsigned indexes. (#50823)
Fixes #44895
1 parent 09e8109 commit 91093fe

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
@@ -963,13 +963,13 @@ end
963963
# This is separate to make it useful even when running with --check-bounds=yes
964964
function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T
965965
i isa Bool && throw(ArgumentError("invalid index: $i of type Bool"))
966-
u = i - r.offset
966+
u = oftype(r.offset, i) - r.offset
967967
T(r.ref + u*r.step)
968968
end
969969

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

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
@@ -2528,3 +2528,10 @@ end
25282528
end
25292529

25302530
end
2531+
2532+
@testset "unsigned index #44895" begin
2533+
x = range(-1,1,length=11)
2534+
@test x[UInt(1)] == -1.0
2535+
a = StepRangeLen(1,2,3,2)
2536+
@test a[UInt(1)] == -1
2537+
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)