Skip to content

Commit

Permalink
Fix splice! to typecheck before alloc/free-ing memory
Browse files Browse the repository at this point in the history
  • Loading branch information
digital-carver committed Apr 11, 2018
1 parent 18ac6e6 commit 2bcb0f6
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1250,12 +1250,15 @@ function splice!(a::Vector, i::Integer, ins=_default_splice)
m = length(ins)
if m == 0
_deleteat!(a, i, 1)
elseif m == 1
a[i] = ins[1]
else
return v
end

# Throw any convert errors before changing the shape of the array
a[i] = ins[1]
if m > 1
_growat!(a, i, m-1)
k = 1
for x in ins
k = 2
for x in ins[2:end]
a[i+k-1] = x
k += 1
end
Expand Down Expand Up @@ -1292,7 +1295,7 @@ julia> A
-1
```
"""
function splice!(a::Vector, r::UnitRange{<:Integer}, ins=_default_splice)
function splice!(a::Vector{T}, r::UnitRange{<:Integer}, ins=_default_splice) where T
v = a[r]
m = length(ins)
if m == 0
Expand All @@ -1305,15 +1308,18 @@ function splice!(a::Vector, r::UnitRange{<:Integer}, ins=_default_splice)
l = last(r)
d = length(r)

# Throw any convert errors before changing the shape of the array
head = convert(T, ins[1])
if m < d
delta = d - m
_deleteat!(a, (f - 1 < n - l) ? f : (l - delta + 1), delta)
elseif m > d
_growat!(a, (f - 1 < n - l) ? f : (l + 1), m - d)
end

k = 1
for x in ins
a[f] = head
k = 2
for x in ins[2:end]
a[f+k-1] = x
k += 1
end
Expand Down

0 comments on commit 2bcb0f6

Please sign in to comment.