Skip to content

Commit 8ea10d3

Browse files
committed
[Release-0.6] Fix missing bounds checks for trailing zero dimensions
This fixes issue #23629 for 0.6. It is done independently from the fix for master (in #23628) due to all the deprecation changes.
1 parent 4aa661a commit 8ea10d3

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

base/abstractarray.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,10 @@ function checkbounds_indices(::Type{Bool}, IA::Tuple, I::Tuple{Any})
407407
end
408408
function checkbounds_linear_indices(::Type{Bool}, IA::Tuple{Vararg{OneTo}}, i)
409409
@_inline_meta
410-
if checkindex(Bool, IA[1], i)
410+
ts = trailingsize(IA)
411+
if checkindex(Bool, IA[1], i) && ts > 0
411412
return true
412-
elseif checkindex(Bool, OneTo(trailingsize(IA)), i) # partial linear indexing
413+
elseif checkindex(Bool, OneTo(ts), i) # partial linear indexing
413414
partial_linear_indexing_warning_lookup(length(IA))
414415
return true # TODO: Return false after the above function is removed in deprecated.jl
415416
end

src/cgutils.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -1726,17 +1726,24 @@ static Value *emit_array_nd_index(const jl_cgval_t &ainfo, jl_value_t *ex, ssize
17261726
// the accessed array, i.e. `if !(i < alen) goto error`.
17271727
if (nidxs > 1) {
17281728
// TODO: REMOVE DEPWARN AND RETURN FALSE AFTER 0.6.
1729-
// We need to check if this is inside the non-linearized size
1729+
// We need to check if this index is inside its non-linearized dimension
17301730
BasicBlock *partidx = BasicBlock::Create(jl_LLVMContext, "partlinidx");
17311731
BasicBlock *partidxwarn = BasicBlock::Create(jl_LLVMContext, "partlinidxwarn");
1732+
BasicBlock *trailingcheck = BasicBlock::Create(jl_LLVMContext, "trailingcheck");
17321733
Value *d = emit_arraysize_for_unsafe_dim(ainfo, ex, nidxs, nd, ctx);
1733-
builder.CreateCondBr(builder.CreateICmpULT(ii, d), endBB, partidx);
1734+
Value *alen = emit_arraylen(ainfo, ex, ctx);
1735+
builder.CreateCondBr(builder.CreateICmpULT(ii, d), trailingcheck, partidx);
1736+
1737+
// If it is inside its own dimension, we still need to ensure all other
1738+
// dimensions are non-zero
1739+
ctx->f->getBasicBlockList().push_back(trailingcheck);
1740+
builder.SetInsertPoint(trailingcheck);
1741+
builder.CreateCondBr(builder.CreateICmpULT(ii, alen), endBB, failBB);
17341742

17351743
// We failed the normal bounds check; check to see if we're
17361744
// inside the linearized size (partial linear indexing):
17371745
ctx->f->getBasicBlockList().push_back(partidx);
17381746
builder.SetInsertPoint(partidx);
1739-
Value *alen = emit_arraylen(ainfo, ex, ctx);
17401747
builder.CreateCondBr(builder.CreateICmpULT(i, alen), partidxwarn, failBB);
17411748

17421749
// We passed the linearized bounds check; now throw the depwarn:

test/arrayops.jl

+5
Original file line numberDiff line numberDiff line change
@@ -2104,3 +2104,8 @@ Base.:(==)(a::T11053, b::T11053) = a.a == b.a
21042104

21052105
#15907
21062106
@test typeof(Array{Int,0}()) == Array{Int,0}
2107+
2108+
@testset "issue 23629" begin
2109+
@test_throws BoundsError zeros(2,3,0)[2,3]
2110+
@test_throws BoundsError checkbounds(zeros(2,3,0), 2, 3)
2111+
end

0 commit comments

Comments
 (0)