-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
Codecov Report
@@ 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
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
lo = ilo-1 | ||
hi = ℵ₀ | ||
@inbounds while lo < hi-1 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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 acceptInteger
instead ofInt
, which improvesBigInt
support. I've also removed the@inbounds
annotations, as this isn't necessarily guaranteed. Since these areO(log n)
operations, the expense of bounds-checking should not be much.