Skip to content

Commit 97f21e3

Browse files
adienesKristofferC
authored andcommitted
fix trailing indices stackoverflow in reinterpreted array (#58293)
would fix #57170, fix #54623 @nanosoldier `runbenchmarks("array", vs=":master")` (cherry picked from commit e853a4f)
1 parent ff0e323 commit 97f21e3

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
@@ -313,13 +313,13 @@ end
313313
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A
314314

315315
# fallbacks
316-
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, I::Vararg{Int, N}) where {T,N}
316+
function _getindex(::IndexSCartesian2, A::AbstractArray, I::Vararg{Int, N}) where {N}
317317
@_propagate_inbounds_meta
318-
getindex(A, I...)
318+
_getindex(IndexCartesian(), A, I...)
319319
end
320-
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) where {T,N}
320+
function _setindex!(::IndexSCartesian2, A::AbstractArray, v, I::Vararg{Int, N}) where {N}
321321
@_propagate_inbounds_meta
322-
setindex!(A, v, I...)
322+
_setindex!(IndexCartesian(), A, v, I...)
323323
end
324324
# fallbacks for array types that use "pass-through" indexing (e.g., `IndexStyle(A) = IndexStyle(parent(A))`)
325325
# but which don't handle SCartesianIndex2

test/reinterpretarray.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,23 @@ test_many_wrappers(fill(1.0, 5, 3), (identity, wrapper)) do a_
320320
@test r[goodinds...] == -5
321321
end
322322
end
323+
324+
let a = rand(ComplexF32, 5)
325+
r = reinterpret(reshape, Float32, a)
326+
ref = Array(r)
327+
328+
@test r[1, :, 1] == ref[1, :]
329+
@test r[1, :, 1, 1, 1] == ref[1, :]
330+
@test r[1, :, UInt8(1)] == ref[1, :]
331+
332+
r[2, :, 1] .= 0f0
333+
ref[2, :] .= 0f0
334+
@test r[2, :, 1] == ref[2, :]
335+
336+
@test r[4] == ref[4]
337+
@test_throws BoundsError r[1, :, 2]
338+
end
339+
323340
let ar = [(1,2), (3,4)]
324341
arr = reinterpret(reshape, Int, ar)
325342
@test @inferred(IndexStyle(arr)) == Base.IndexSCartesian2{2}()
@@ -607,3 +624,9 @@ let R = reinterpret(reshape, Float32, ComplexF32[1.0f0+2.0f0*im, 4.0f0+3.0f0*im]
607624
@test !isassigned(R, 5)
608625
@test Array(R)::Matrix{Float32} == [1.0f0 4.0f0; 2.0f0 3.0f0]
609626
end
627+
628+
@testset "issue #54623" begin
629+
x = 0xabcdef01234567
630+
@test reinterpret(reshape, UInt8, fill(x)) == [0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00]
631+
@test reinterpret(reshape, UInt8, [x]) == [0x67; 0x45; 0x23; 0x01; 0xef; 0xcd; 0xab; 0x00;;]
632+
end

0 commit comments

Comments
 (0)