From 4a24bf8744922263c957baca43f8616d47a39131 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Sat, 4 Nov 2023 06:22:45 -0400 Subject: [PATCH] fix out-of-bounds bug in BufferedVector (#91) * fix out-of-bounds bug in BufferedVector --------- Co-authored-by: Drvi --- src/BufferedVectors.jl | 4 ++-- test/BufferedVectors.jl | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/BufferedVectors.jl b/src/BufferedVectors.jl index 70456fe..b7cca56 100644 --- a/src/BufferedVectors.jl +++ b/src/BufferedVectors.jl @@ -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 \ No newline at end of file +end # module diff --git a/test/BufferedVectors.jl b/test/BufferedVectors.jl index ffa2ae7..7a0c41b 100644 --- a/test/BufferedVectors.jl +++ b/test/BufferedVectors.jl @@ -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 @@ -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}() @@ -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)