Skip to content

Commit 1263513

Browse files
authored
Fix for issue 194 (#195)
The indexing operation A[] gets mapped to A[1], which may not be a valid index for OffsetVectors. This PR fixes the bounds-checking for this.
1 parent b20b4db commit 1263513

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.5.1"
3+
version = "1.5.2"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/axes.jl

+10
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, "OffsetArrays.IdOffsetRange(",fi
179179
# Optimizations
180180
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)
181181

182+
# issue 194
183+
# The indexing operation A[] gets mapped to A[1].
184+
# The bounds-checking for this needs to be handled separately for AbstractVectors
185+
# See https://github.com/JuliaLang/julia/issues/39379 for this issue reported in Base
186+
# Once a PR fixing it is merged, we may limit our fix to earlier Julia versions
187+
@inline function Base.checkbounds_indices(::Type{Bool}, IA::Tuple{IdOffsetRange}, ::Tuple{})
188+
x = IA[1]
189+
length(x) == 1 && first(x) == one(eltype(x))
190+
end
191+
182192
if VERSION < v"1.5.2"
183193
# issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
184194
# fixed by https://github.com/JuliaLang/julia/pull/37204

test/runtests.jl

+16
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,22 @@ end
711711
@test setindex!(A, 2, 4) === A
712712
@test A[4] == 2
713713
end
714+
715+
@testset "issue 194" begin
716+
A = OffsetArray([0], 1);
717+
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
718+
@test_throws BoundsError A[]
719+
A = OffsetArray([6], 1:1)
720+
@test A[] == 6
721+
722+
A = OffsetArray(reshape(1:4, 2, 2), 2, 2);
723+
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
724+
@test_throws BoundsError A[]
725+
A = OffsetArray(reshape([6], 1, 1), 1:1, 1:1);
726+
@test A[] == 6
727+
A = OffsetArray(A, 1:1, 2:2);
728+
@test A[] == 6
729+
end
714730
end
715731

716732
@testset "Vector indexing" begin

0 commit comments

Comments
 (0)