Skip to content

Commit

Permalink
fix out-of-bounds bug in BufferedVector (#91)
Browse files Browse the repository at this point in the history
* fix out-of-bounds bug in BufferedVector
---------

Co-authored-by: Drvi <[email protected]>
  • Loading branch information
vtjnash and Drvi authored Nov 4, 2023
1 parent 54fab55 commit 4a24bf8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/BufferedVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ function shiftleft!(x::BufferedVector, n)
empty!(x)
return nothing
end
unsafe_copyto!(x.elements, 1, x.elements, 1 + n, len - n + 1)
unsafe_copyto!(x.elements, 1, x.elements, 1 + n, len - n)
x.occupied -= n
return nothing
end

end # module
end # module
7 changes: 6 additions & 1 deletion test/BufferedVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ using SentinelArrays: BufferedVectors
unsafe_push!(bv, 42)
@test bv == [1, 42]

# --check-bounds=auto not available prior to Julia 1.8, so we force the default
# bounds check behavior by removing the flag from the command line
julia_cmd_default_boundscheck = Base.julia_cmd()
filter!(x->!startswith(x, "--check-bounds="), julia_cmd_default_boundscheck.exec)
code = """
using SentinelArrays
using Test
Expand All @@ -67,7 +71,7 @@ using SentinelArrays: BufferedVectors
@noinline unsafe(bv, i) = @inbounds bv[i]
@test unsafe(bv, 2) == bv.elements[2]
"""
cmd = `$(Base.julia_cmd()) --startup-file=no --project=. --check-bounds=auto -e $code`
cmd = `$(julia_cmd_default_boundscheck) --startup-file=no --project=. -e $code`
@test success(pipeline(cmd; stdout=stdout, stderr=stderr))

bv = BufferedVector{Int32}()
Expand All @@ -78,6 +82,7 @@ using SentinelArrays: BufferedVectors
bv = BufferedVector{Int}([1,2,3], 3)
shiftleft!(bv, 1)
@test bv == [2,3]
@test bv.elements[3] == 3 # the last element is not overwritten

bv = BufferedVector{Int}([1,2,3], 3)
shiftleft!(bv, 2)
Expand Down

0 comments on commit 4a24bf8

Please sign in to comment.