Skip to content

Commit

Permalink
Move bounds checks on copyto!(dst, n, src) (#43517)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed May 10, 2022
1 parent c69a202 commit eb938da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 11 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,17 @@ end

function copyto!(dest::AbstractArray, dstart::Integer, src)
i = Int(dstart)
for x in src
dest[i] = x
i += 1
if haslength(src) && length(dest) > 0
@boundscheck checkbounds(dest, i:(i + length(src) - 1))
for x in src
@inbounds dest[i] = x
i += 1
end
else
for x in src
dest[i] = x
i += 1
end
end
return dest
end
Expand Down
10 changes: 10 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,16 @@ end
@test_throws ArgumentError LinearAlgebra.copy_transpose!(a,2:3,1:3,b,1:5,2:7)
end

@testset "empty copyto!" begin
@test isempty(copyto!(Int[], ()))
@test isempty(copyto!(Int[], Int[]))
@test copyto!([1,2], ()) == [1,2]

@test isempty(copyto!(Int[], 1, ()))
@test isempty(copyto!(Int[], 1, Int[]))
@test copyto!([1,2], 1, ()) == [1,2]
end

module RetTypeDecl
using Test
import Base: +, *, broadcast, convert
Expand Down

0 comments on commit eb938da

Please sign in to comment.