Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indexing BigInt axes with large indices #142

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "InfiniteArrays"
uuid = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
version = "0.13.1"
version = "0.13.2"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
6 changes: 3 additions & 3 deletions src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ axistype(::OneToInf{V}, ::OneTo{T}) where {T,V} = OneToInf{promote_type(T,V)}()
# returns the range of indices of v equal to x
# if v does not contain x, returns a 0-length range
# indicating the insertion point of x
function searchsorted(v::AbstractVector, x, ilo::Int, ::PosInfinity, o::Ordering)
function searchsorted(v::AbstractVector, x, ilo::Integer, ::PosInfinity, o::Ordering)
lo = ilo-1
hi = ℵ₀
@inbounds while lo < hi-1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were the inbounds removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The access may not be within bounds, unless we consider this to be an internal method that is never directly called by a user. Otherwise, a call like

julia> v = [1,2]
2-element Vector{Int64}:
 1
 2

julia> searchsorted(v, 2, firstindex(v), ∞, Base.Order.ForwardOrdering())
ERROR: BoundsError: attempt to access 2-element Vector{Int64} at index [1000]

may lead to memory corruption through out-of-bounds access.

The alternative is to add a bounds-check to the function, in which case we may retain the inbounds annotations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I copied this code from Base which has the exact same issue:

julia> searchsorted([1,2], 2, 1, 10, Base.Order.ForwardOrdering())
2:3

I've started an issue: JuliaLang/julia#51176

I think its best to just check that hi ≤ length(v) here and keep the inbounds

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reverted the inbounds for now. This is unlikely to be an issue in practice, as these are internal methods. We may revisit this in the future, if necessary.

Expand All @@ -182,7 +182,7 @@ end

# index of the first value of vector a that is greater than or equal to x;
# returns length(v)+1 if x is greater than all values in v.
function searchsortedfirst(v::AbstractVector, x, lo::Int, hi::PosInfinity, o::Ordering)
function searchsortedfirst(v::AbstractVector, x, lo::Integer, hi::PosInfinity, o::Ordering)
u = 1
lo = lo - u
hi = ℵ₀
Expand All @@ -199,7 +199,7 @@ end

# index of the last value of vector a that is less than or equal to x;
# returns 0 if x is less than all values of v.
function searchsortedlast(v::AbstractVector, x, lo::Int, hi::PosInfinity, o::Ordering)
function searchsortedlast(v::AbstractVector, x, lo::Integer, hi::PosInfinity, o::Ordering)
u = 1
lo = lo - u
hi = ℵ₀
Expand Down
1 change: 1 addition & 0 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ AbstractVector{T}(a::OneToInf) where T<:Real = InfUnitRange{T}(a)
## interface implementations

size(r::InfRanges) = (ℵ₀,)
axes(r::InfRanges{<:Integer}) = (OneToInf{promote_type(Int,eltype(r))}(),)
axes(r::InfRanges) = (OneToInf(),)

isempty(r::InfRanges) = false
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ end
@test ∞:1 ≡ 1:0

@testset "indexing" begin
@testset "axes" begin
r = axes(big(1):∞,1)
@test r == axes(r,1)
@test r[typemax(Int)+big(1)] == typemax(Int)+big(1)
end
L32 = @inferred(Int32(1):∞)
L64 = @inferred(Int64(1):∞)
@test @inferred(L32[1]) === Int32(1) && @inferred(L64[1]) === Int64(1)
Expand Down