Skip to content

Commit

Permalink
Fix overflow in searchsorted (JuliaLang#19432)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne authored Nov 29, 2016
1 parent 7f230ad commit b7ac7ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 12 additions & 6 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,24 @@ function searchsortedfirst{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering)
end

function searchsortedfirst{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering)
if step(a) == 0
lt(o, first(a), x) ? length(a) + 1 : 1
if lt(o, first(a), x)
if step(a) == 0
length(a) + 1
else
min(cld(x - first(a), step(a)), length(a)) + 1
end
else
clamp(-fld(first(a) - signed(x), step(a)) + 1, 1, length(a) + 1)
1
end
end

function searchsortedlast{T<:Integer}(a::Range{T}, x::Unsigned, o::DirectOrdering)
if step(a) == 0
lt(o, x, first(a)) ? 0 : length(a)
if lt(o, x, first(a))
0
elseif step(a) == 0
length(a)
else
clamp( fld(signed(x) - first(a), step(a)) + 1, 0, length(a))
min(fld(x - first(a), step(a)) + 1, length(a))
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,7 @@ end
# PR #18791
@test sort([typemax(Int),typemin(Int)]) == [typemin(Int),typemax(Int)]
@test sort([typemax(UInt),0]) == [0,typemax(UInt)]

# issue #19005
@test searchsortedfirst(0:256, 0x80) == 129
@test searchsortedlast(0:256, 0x80) == 129

0 comments on commit b7ac7ec

Please sign in to comment.