Skip to content

Commit

Permalink
deleteat! : check bounds for the first passed index (#36231)
Browse files Browse the repository at this point in the history
* deleteat! : check bounds for the first passed index

All other indices are bound-checked.
Currently, the behavior looks like:
```julia
julia> deleteat!([1:1000;], [-100])

signal (11): Segmentation fault
[...]

julia>  deleteat!(BigInt[0], [0])
free(): invalid next size (normal)

signal (6): Aborted
[...]

julia> deleteat!(BigInt[0], [-100])
ERROR: UndefRefError: access to undefined reference
[...]

julia> deleteat!([0], [0])
Int64[]

julia> deleteat!([0], [2])
1-element Array{Int64,1}:
 0

julia> deleteat!([0], [3])
ERROR: InexactError: check_top_bit(UInt64, -1)
[...]
```
With this commit, all these expressions throw a `BoundsError`.

* Update base/array.jl

Co-authored-by: Simeon Schaub <[email protected]>

Co-authored-by: Simeon Schaub <[email protected]>
  • Loading branch information
rfourquet and simeonschaub authored Jun 16, 2020
1 parent 33659c7 commit 6cdfcf9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1352,9 +1352,9 @@ function _deleteat!(a::Vector, inds, dltd=Nowhere())
n = length(a)
y = iterate(inds)
y === nothing && return a
n == 0 && throw(BoundsError(a, inds))
(p, s) = y
p <= n && push!(dltd, @inbounds a[p])
checkbounds(a, p)
push!(dltd, @inbounds a[p])
q = p+1
while true
y = iterate(inds, s)
Expand Down
6 changes: 5 additions & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1448,11 +1448,15 @@ end
@test deleteat!(a, [1,3,5,7:10...]) == [2,4,6]
@test_throws BoundsError deleteat!(a, 13)
@test_throws BoundsError deleteat!(a, [1,13])
@test_throws ArgumentError deleteat!(a, [5,3])
@test_throws ArgumentError deleteat!(a, [3,2]) # not sorted
@test_throws BoundsError deleteat!(a, 5:20)
@test_throws BoundsError deleteat!(a, Bool[])
@test_throws BoundsError deleteat!(a, [true])
@test_throws BoundsError deleteat!(a, falses(11))
@test_throws BoundsError deleteat!(a, [0])
@test_throws BoundsError deleteat!(a, [4])
@test_throws BoundsError deleteat!(a, [5])
@test_throws BoundsError deleteat!(a, [5, 3])

@test_throws BoundsError deleteat!([], 1)
@test_throws BoundsError deleteat!([], [1])
Expand Down

1 comment on commit 6cdfcf9

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

Please sign in to comment.