Skip to content

Commit e853a4f

Browse files
authored
fix trailing indices stackoverflow in reinterpreted array (#58293)
would fix #57170, fix #54623 @nanosoldier `runbenchmarks("array", vs=":master")`
1 parent 9039555 commit e853a4f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

base/reinterpretarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ end
314314
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A
315315

316316
# fallbacks
317-
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, I::Vararg{Int, N}) where {T,N}
317+
function _getindex(::IndexSCartesian2, A::AbstractArray, I::Vararg{Int, N}) where {N}
318318
@_propagate_inbounds_meta
319-
getindex(A, I...)
319+
_getindex(IndexCartesian(), A, I...)
320320
end
321-
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) where {T,N}
321+
function _setindex!(::IndexSCartesian2, A::AbstractArray, v, I::Vararg{Int, N}) where {N}
322322
@_propagate_inbounds_meta
323-
setindex!(A, v, I...)
323+
_setindex!(IndexCartesian(), A, v, I...)
324324
end
325325
# fallbacks for array types that use "pass-through" indexing (e.g., `IndexStyle(A) = IndexStyle(parent(A))`)
326326
# but which don't handle SCartesianIndex2

test/reinterpretarray.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,23 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
325325
@test r[goodinds...] == -5
326326
end
327327
end
328+
329+
let a = rand(ComplexF32, 5)
330+
r = reinterpret(reshape, Float32, a)
331+
ref = Array(r)
332+
333+
@test r[1, :, 1] == ref[1, :]
334+
@test r[1, :, 1, 1, 1] == ref[1, :]
335+
@test r[1, :, UInt8(1)] == ref[1, :]
336+
337+
r[2, :, 1] .= 0f0
338+
ref[2, :] .= 0f0
339+
@test r[2, :, 1] == ref[2, :]
340+
341+
@test r[4] == ref[4]
342+
@test_throws BoundsError r[1, :, 2]
343+
end
344+
328345
let ar = [(1,2), (3,4)]
329346
arr = reinterpret(reshape, Int, ar)
330347
@test @inferred(IndexStyle(arr)) == Base.IndexSCartesian2{2}()
@@ -612,3 +629,9 @@ let R = reinterpret(reshape, Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im]
612629
@test !isassigned(R, 5)
613630
@test Array(R)::Matrix{Float32} == [1.0f0 4.0f0; 2.0f0 3.0f0]
614631
end
632+
633+
@testset "issue #54623" begin
634+
x = 0xabcdef01234567
635+
@test reinterpret(reshape, UInt8, fill(x)) == [0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00]
636+
@test reinterpret(reshape, UInt8, [x]) == [0x67; 0x45; 0x23; 0x01; 0xef; 0xcd; 0xab; 0x00;;]
637+
end

0 commit comments

Comments
 (0)