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

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Aug 28, 2023

This uses type-promotion for integer ranges to widen the axis type, which makes the result correct for large BigInt arguments. I don't like the special-casing, but I couldn't figure out how to work around floating-point ranges.

This required changing certain searchsorted methods to accept Integer instead of Int, which improves BigInt support. I've also removed the @inbounds annotations, as this isn't necessarily guaranteed. Since these are O(log n) operations, the expense of bounds-checking should not be much.

@codecov
Copy link

codecov bot commented Aug 28, 2023

Codecov Report

Merging #142 (ae64dc9) into master (8fe1160) will increase coverage by 0.03%.
Report is 3 commits behind head on master.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #142      +/-   ##
==========================================
+ Coverage   85.89%   85.92%   +0.03%     
==========================================
  Files           6        6              
  Lines         730      732       +2     
==========================================
+ Hits          627      629       +2     
  Misses        103      103              
Files Changed Coverage Δ
src/InfiniteArrays.jl 86.66% <100.00%> (ø)
src/infrange.jl 87.57% <100.00%> (+0.07%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jishnub jishnub requested a review from dlfivefifty August 28, 2023 07:45
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.

@dlfivefifty dlfivefifty merged commit 9360eb6 into JuliaArrays:master Sep 6, 2023
@jishnub jishnub deleted the axesindexingbig branch September 6, 2023 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants