Skip to content

Commit eb96c07

Browse files
LilithHafnerKristofferC
authored and
KristofferC
committed
Test and fix non-int-length bug in view(::Memory, ::Union{UnitRange, Base.OneTo}) (#53991)
We assumed, falsely, that `length(inds) isa Int`. The length must be convertible to an `Int` or we throw, but that conversion may need to be explicitly performed. Fixes #53990 CC @oscardssmith @vtjnash @odow (cherry picked from commit e4f2124)
1 parent 146a7db commit eb96c07

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

base/genericmemory.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ end
308308
isempty(inds) && return T[] # needed to allow view(Memory{T}(undef, 0), 2:1)
309309
@boundscheck checkbounds(m, inds)
310310
ref = MemoryRef(m, first(inds)) # @inbounds would be safe here but does not help performance.
311-
dims = (length(inds),)
311+
dims = (Int(length(inds)),)
312312
$(Expr(:new, :(Array{T, 1}), :ref, :dims))
313313
end
314314
end

test/arrayops.jl

+3
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,9 @@ end
32053205
@test @inferred(view(mem, -5:-7))::Vector{Int} == []
32063206
@test @inferred(reshape(mem, 5, 2))::Matrix{Int} == reshape(11:20, 5, 2)
32073207

3208+
# 53990
3209+
@test @inferred(view(mem, unsigned(1):10))::Vector{Int} == 11:20
3210+
32083211
empty_mem = Memory{Module}(undef, 0)
32093212
@test_throws BoundsError view(empty_mem, 0:1)
32103213
@test_throws BoundsError view(empty_mem, 1:2)

test/core.jl

+3
Original file line numberDiff line numberDiff line change
@@ -5524,6 +5524,9 @@ let a = Base.StringVector(2^17)
55245524
@test sizeof(c) == 0
55255525
end
55265526

5527+
# issue #53990 / https://github.com/JuliaLang/julia/pull/53896#discussion_r1555087951
5528+
@test Base.StringVector(UInt64(2)) isa Vector{UInt8}
5529+
55275530
@test_throws ArgumentError eltype(Bottom)
55285531

55295532
# issue #16424, re-evaluating type definitions

0 commit comments

Comments
 (0)