Skip to content

Commit c30199f

Browse files
authored
Bugfix: Use Base.aligned_sizeof instead of sizeof in Mmap.mmap (#58998)
fix #58982
1 parent bea90a2 commit c30199f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

stdlib/Mmap/src/Mmap.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,16 @@ like HDF5 (which can be used with memory-mapping).
187187
"""
188188
function mmap(io::IO,
189189
::Type{Array{T,N}}=Vector{UInt8},
190-
dims::NTuple{N,Integer}=(div(filesize(io)-position(io),sizeof(T)),),
190+
dims::NTuple{N,Integer}=(div(filesize(io)-position(io),Base.aligned_sizeof(T)),),
191191
offset::Integer=position(io); grow::Bool=true, shared::Bool=true) where {T,N}
192192
# check inputs
193193
isopen(io) || throw(ArgumentError("$io must be open to mmap"))
194194
isbitstype(T) || throw(ArgumentError("unable to mmap $T; must satisfy isbitstype(T) == true"))
195195

196-
len = sizeof(T)
196+
len = Base.aligned_sizeof(T)
197197
for l in dims
198198
len, overflow = Base.Checked.mul_with_overflow(promote(len, l)...)
199-
overflow && throw(ArgumentError("requested size prod($((sizeof(T), dims...))) too large, would overflow typeof(size(T)) == $(typeof(len))"))
199+
overflow && throw(ArgumentError("requested size prod($((len, dims...))) too large, would overflow typeof(size(T)) == $(typeof(len))"))
200200
end
201201
len >= 0 || throw(ArgumentError("requested size must be ≥ 0, got $len"))
202202
len == 0 && return Array{T}(undef, ntuple(x->0,Val(N)))
@@ -267,7 +267,7 @@ end
267267

268268
mmap(file::AbstractString,
269269
::Type{T}=Vector{UInt8},
270-
dims::NTuple{N,Integer}=(div(filesize(file),sizeof(eltype(T))),),
270+
dims::NTuple{N,Integer}=(div(filesize(file),Base.aligned_sizeof(eltype(T))),),
271271
offset::Integer=Int64(0); grow::Bool=true, shared::Bool=true) where {T<:Array,N} =
272272
open(io->mmap(io, T, dims, offset; grow=grow, shared=shared), file, isfile(file) ? "r" : "w+")::Array{eltype(T),N}
273273

stdlib/Mmap/test/runtests.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ s = open(file)
4444
@test length(@inferred mmap(s, Vector{Int8}, 12, 0; grow=false)) == 12
4545
@test length(@inferred mmap(s, Vector{Int8}, 12, 0; shared=false)) == 12
4646
close(s)
47-
@test_throws ErrorException mmap(file, Vector{Ref}) # must be bit-type
47+
@test_throws ArgumentError mmap(file, Vector{Ref}) # must be bit-type
4848
GC.gc(); GC.gc()
4949

5050
file = tempname() # new name to reduce chance of issues due slow windows fs
@@ -343,6 +343,19 @@ end
343343
GC.gc()
344344
rm(file)
345345

346+
@testset "test for #58982 - mmap with primitive types" begin
347+
file = tempname()
348+
primitive type PrimType9Bytes 9*8 end
349+
arr = Vector{PrimType9Bytes}(undef, 2)
350+
write(file, arr)
351+
m = mmap(file, Vector{PrimType9Bytes})
352+
@test length(m) == 2
353+
@test m[1] == arr[1]
354+
@test m[2] == arr[2]
355+
finalize(m); m = nothing; GC.gc()
356+
rm(file)
357+
end
358+
346359
@testset "Docstrings" begin
347360
@test isempty(Docs.undocumented_names(Mmap))
348361
end

0 commit comments

Comments
 (0)