Skip to content

Integer overflow in reverse! for an OffsetArray #45870

@jishnub

Description

@jishnub
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions