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

Integer overflow in reverse! for an OffsetArray #45870

Closed
jishnub opened this issue Jun 30, 2022 · 1 comment · Fixed by #45871
Closed

Integer overflow in reverse! for an OffsetArray #45870

jishnub opened this issue Jun 30, 2022 · 1 comment · Fixed by #45871

Comments

@jishnub
Copy link
Contributor

jishnub commented Jun 30, 2022

julia> v = [1:4;]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> reverse!(v, firstindex(v))
4-element Vector{Int64}:
 4
 3
 2
 1

julia> vo = OffsetArray([1:4;], typemax(Int)-4)
4-element OffsetArray(::Vector{Int64}, 9223372036854775804:9223372036854775807) with eltype Int64 with indices 9223372036854775804:9223372036854775807:
 1
 2
 3
 4

julia> reverse!(vo, firstindex(vo))
4-element OffsetArray(::Vector{Int64}, 9223372036854775804:9223372036854775807) with eltype Int64 with indices 9223372036854775804:9223372036854775807:
 1
 2
 3
 4

julia> versioninfo()
Julia Version 1.9.0-DEV.866
Commit 8419acc5ba* (2022-06-30 02:57 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.5 (ORCJIT, tigerlake)
  Threads: 1 on 8 virtual cores
Environment:
  LD_LIBRARY_PATH = :/usr/lib/x86_64-linux-gnu/gtk-3.0/modules
  JULIA_EDITOR = vim

This possibly happens because there is an overflow in

@inbounds for i in s:div(s+n-1, 2)

while evaluating div(s+n-1, 2).

julia> s = firstindex(vo)
9223372036854775804

julia> n = lastindex(vo)
9223372036854775807

julia> div(s + n - 1, 2)
-3
@jishnub
Copy link
Contributor Author

jishnub commented Jun 30, 2022

Should we include something like Base.Sort.midpoint in Base? This seems useful, and may help avoid such overflow bugs.

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 a pull request may close this issue.

1 participant