Skip to content

Commit

Permalink
Fix integer overflow in reverse! (#45871)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3c04919)
  • Loading branch information
jishnub authored and KristofferC committed Jul 6, 2022
1 parent a2868fb commit ca72b59
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
28 changes: 17 additions & 11 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,11 @@ function reverseind(a::AbstractVector, i::Integer)
first(li) + last(li) - i
end

# This implementation of `midpoint` is performance-optimized but safe
# only if `lo <= hi`.
midpoint(lo::T, hi::T) where T<:Integer = lo + ((hi - lo) >>> 0x01)
midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...)

"""
reverse!(v [, start=1 [, stop=length(v) ]]) -> v
Expand Down Expand Up @@ -1861,17 +1866,18 @@ julia> A
"""
function reverse!(v::AbstractVector, start::Integer, stop::Integer=lastindex(v))
s, n = Int(start), Int(stop)
liv = LinearIndices(v)
if n <= s # empty case; ok
elseif !(first(liv) s last(liv))
throw(BoundsError(v, s))
elseif !(first(liv) n last(liv))
throw(BoundsError(v, n))
end
r = n
@inbounds for i in s:div(s+n-1, 2)
v[i], v[r] = v[r], v[i]
r -= 1
if n > s # non-empty and non-trivial
liv = LinearIndices(v)
if !(first(liv) s last(liv))
throw(BoundsError(v, s))
elseif !(first(liv) n last(liv))
throw(BoundsError(v, n))
end
r = n
@inbounds for i in s:midpoint(s, n-1)
v[i], v[r] = v[r], v[i]
r -= 1
end
end
return v
end
Expand Down
7 changes: 1 addition & 6 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using .Base: copymutable, LinearIndices, length, (:),
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !,
extrema, sub_with_overflow, add_with_overflow, oneunit, div, getindex, setindex!,
length, resize!, fill, Missing, require_one_based_indexing, keytype,
UnitRange, max, min
UnitRange, max, min, midpoint

using .Base: >>>, !==

Expand Down Expand Up @@ -166,11 +166,6 @@ same thing as `partialsort!` but leaving `v` unmodified.
partialsort(v::AbstractVector, k::Union{Integer,OrdinalRange}; kws...) =
partialsort!(copymutable(v), k; kws...)

# This implementation of `midpoint` is performance-optimized but safe
# only if `lo <= hi`.
midpoint(lo::T, hi::T) where T<:Integer = lo + ((hi - lo) >>> 0x01)
midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...)

# reference on sorted binary search:
# http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary

Expand Down
17 changes: 17 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,23 @@ rv = reverse(v)
cv = copy(v)
@test reverse!(cv) == rv

@testset "reverse! (issue #45870)" begin
@testset for n in [4,5]
offset = typemax(Int)-n
vo = OffsetArray([1:n;], offset)
vo2 = OffsetArray([1:n;], offset)
@test reverse!(vo) == OffsetArray(n:-1:1, offset)
@test reverse!(vo) == vo2
@test_throws BoundsError reverse!(vo, firstindex(vo)-1, firstindex(vo))
@test reverse!(vo, firstindex(vo), firstindex(vo)-1) == vo2
@test reverse!(vo, firstindex(vo), firstindex(vo)) == vo2
@test reverse!(vo, lastindex(vo), lastindex(vo)) == vo2
@test reverse!(vo, lastindex(vo), lastindex(vo)+1) == vo2 # overflow in stop
@test reverse!(vo, firstindex(vo)+1) == OffsetArray([1;n:-1:2], offset)
@test reverse!(vo2, firstindex(vo)+1, lastindex(vo)-1) == OffsetArray([1;n-1:-1:2;n], offset)
end
end

A = OffsetArray(rand(4,4), (-3,5))
@test lastindex(A) == 16
@test lastindex(A, 1) == 1
Expand Down

0 comments on commit ca72b59

Please sign in to comment.