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

OffsetArray accesses out-of-bounds memory and returns an incorrect value when indexing with [] #194

Closed
yurivish opened this issue Jan 23, 2021 · 1 comment · Fixed by #195

Comments

@yurivish
Copy link

yurivish commented Jan 23, 2021

It is possible that this happens due to an incorrect use of @inbounds:

julia> using OffsetArrays

julia> OffsetArray([0], 1)[]
4562921808

julia> [OffsetArray([0], 1)[] for _ in 1:10]
10-element Array{Int64,1}:
           0
  4396158144
  4582191936
  4582191936
  4396158144
           0
  4396158144
  8589934592
 12884901888
           0

julia> OffsetArray([0], 100:100)[]
4614560496
julia> versioninfo()
Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
@jishnub
Copy link
Member

jishnub commented Jan 24, 2021

This is indeed due to @inbounds, but this is actually a bug in bounds-checking. However I'm not sure if the fix should be in OffsetArrays or in Base. The bounds-check in Base uses

checkbounds_indices(::Type{Bool}, IA::Tuple, ::Tuple{}) = (@_inline_meta; all(x->unsafe_length(x)==1, IA))

which seems to assume 1-based indexing.

Perhaps a fix might be added here for older Julia versions, and the main fix might go into Base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants