Skip to content

Commit c473aef

Browse files
Merge pull request #23 from JuliaReinforcementLearning/jpsl/bounds
"Fix bounds error in CircularArrayBuffer"
2 parents 67b944b + 4cb702a commit c473aef

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/CircularArrayBuffers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Base.IndexStyle(::CircularArrayBuffer) = IndexLinear()
4545

4646
Base.size(cb::CircularArrayBuffer{T,N}, i::Integer) where {T,N} = i == N ? cb.nframes : size(cb.buffer, i)
4747
Base.size(cb::CircularArrayBuffer{T,N}) where {T,N} = ntuple(i -> size(cb, i), N)
48-
Base.getindex(cb::CircularArrayBuffer{T,N}, i::Int) where {T,N} = getindex(cb.buffer, _buffer_index(cb, i))
48+
Base.getindex(cb::CircularArrayBuffer{T,N}, i::Int) where {T,N} = (@boundscheck checkbounds(cb, i); getindex(cb.buffer, _buffer_index(cb, i)))
4949
Base.getindex(cb::CircularArrayBuffer{T,N}, I...) where {T,N} = getindex(cb.buffer, Base.front(I)..., _buffer_frame(cb, Base.last(I)))
5050

5151
# !!!

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ CUDA.allowscalar(false)
2323
@test_throws BoundsError @view b[:, 9]
2424
end
2525

26+
@testset "Bounds error for zero-length buffer" begin
27+
b = CircularVectorBuffer{Bool}(10)
28+
@test_throws BoundsError b[end]
29+
for i in 1:5
30+
push!(b, true)
31+
end
32+
@test b[end] == true
33+
end
34+
2635
@testset "1D vector" begin
2736
b = CircularArrayBuffer([[1], [2, 3]])
2837
push!(b, [4, 5, 6])

0 commit comments

Comments
 (0)