Skip to content

Commit 6cdfcf9

Browse files
deleteat! : check bounds for the first passed index (#36231)
* 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 <simeondavidschaub99@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
1 parent 33659c7 commit 6cdfcf9

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

base/array.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,9 @@ function _deleteat!(a::Vector, inds, dltd=Nowhere())
13521352
n = length(a)
13531353
y = iterate(inds)
13541354
y === nothing && return a
1355-
n == 0 && throw(BoundsError(a, inds))
13561355
(p, s) = y
1357-
p <= n && push!(dltd, @inbounds a[p])
1356+
checkbounds(a, p)
1357+
push!(dltd, @inbounds a[p])
13581358
q = p+1
13591359
while true
13601360
y = iterate(inds, s)

test/arrayops.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,11 +1448,15 @@ end
14481448
@test deleteat!(a, [1,3,5,7:10...]) == [2,4,6]
14491449
@test_throws BoundsError deleteat!(a, 13)
14501450
@test_throws BoundsError deleteat!(a, [1,13])
1451-
@test_throws ArgumentError deleteat!(a, [5,3])
1451+
@test_throws ArgumentError deleteat!(a, [3,2]) # not sorted
14521452
@test_throws BoundsError deleteat!(a, 5:20)
14531453
@test_throws BoundsError deleteat!(a, Bool[])
14541454
@test_throws BoundsError deleteat!(a, [true])
14551455
@test_throws BoundsError deleteat!(a, falses(11))
1456+
@test_throws BoundsError deleteat!(a, [0])
1457+
@test_throws BoundsError deleteat!(a, [4])
1458+
@test_throws BoundsError deleteat!(a, [5])
1459+
@test_throws BoundsError deleteat!(a, [5, 3])
14561460

14571461
@test_throws BoundsError deleteat!([], 1)
14581462
@test_throws BoundsError deleteat!([], [1])

0 commit comments

Comments
 (0)