Skip to content

Commit dcb01ae

Browse files
committed
Fix #3658
1 parent beeb195 commit dcb01ae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

base/datafmt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ readdlm(input, dlm::Char, T::Type, eol::Char; opts...) = readdlm_auto(input, dlm
4343

4444
function readdlm_auto(input, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
4545
optsd = val_opts(opts)
46-
isa(input, String) && (input = get(optsd, :use_mmap, is_unix(OS_NAME)) ? mmap_array(Uint8, (filesize(input),), open(input, "r")) : readall(input))
46+
isa(input, String) && (fsz = filesize(input); input = get(optsd, :use_mmap, true) && fsz < typemax(Int) ? mmap_array(Uint8, (int(fsz),), open(input, "r")) : readall(input))
4747
sinp = isa(input, Vector{Uint8}) ? ccall(:jl_array_to_string, ByteString, (Array{Uint8,1},), input) :
4848
isa(input, IO) ? readall(input) :
4949
input

base/mmap.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ function mmap_stream_settings(s::IO)
108108
end
109109

110110
# Mmapped-array constructor
111-
function mmap_array{T,N}(::Type{T}, dims::NTuple{N,Int}, s::IO, offset::FileOffset)
111+
function mmap_array{T,N,TInt<:Integer}(::Type{T}, dims::NTuple{N,TInt}, s::IO, offset::FileOffset)
112112
prot, flags, iswrite = mmap_stream_settings(s)
113113
len = prod(dims)*sizeof(T)
114+
if len > typemax(Int)
115+
error("File is too large to memory-map on this platform")
116+
end
114117
if iswrite
115118
pmap, delta = mmap_grow(len, prot, flags, fd(s), offset)
116119
else
@@ -122,13 +125,16 @@ function mmap_array{T,N}(::Type{T}, dims::NTuple{N,Int}, s::IO, offset::FileOffs
122125
end
123126

124127
# Mmapped-bitarray constructor
125-
function mmap_bitarray{N}(dims::NTuple{N,Int}, s::IOStream, offset::FileOffset)
128+
function mmap_bitarray{N,TInt<:Integer}(dims::NTuple{N,TInt}, s::IOStream, offset::FileOffset)
126129
prot, flags, iswrite = mmap_stream_settings(s)
127130
if length(dims) == 0
128131
dims = 0
129132
end
130133
n = prod(dims)
131134
nc = num_bit_chunks(n)
135+
if nc > typemax(Int)
136+
error("File is too large to memory-map on this platform")
137+
end
132138
chunks = mmap_array(Uint64, (nc,), s, offset)
133139
if iswrite
134140
chunks[end] &= @_msk_end n
@@ -152,11 +158,15 @@ end
152158
### Windows implementation ###
153159
@windows_only begin
154160
# Mmapped-array constructor
155-
function mmap_array{T,N}(::Type{T}, dims::NTuple{N,Int}, s::IO, offset::FileOffset)
161+
function mmap_array{T,N,TInt<:Integer}(::Type{T}, dims::NTuple{N,TInt}, s::IO, offset::FileOffset)
156162
shandle = _get_osfhandle(RawFD(fd(s)))
157163
ro = isreadonly(shandle)
158164
flprotect = ro ? 0x02 : 0x04
159-
szarray = convert(Csize_t, prod(dims))*sizeof(T)
165+
len = prod(dims)*sizeof(T)
166+
if len > typemax(Int)
167+
error("File is too large to memory-map on this platform")
168+
end
169+
szarray = convert(Csize_t, len)
160170
szfile = szarray + convert(Csize_t, offset)
161171
mmaphandle = ccall(:CreateFileMappingA, stdcall, Ptr{Void}, (Ptr{Void}, Ptr{Void}, Cint, Cint, Cint, Ptr{Void}), shandle.handle, C_NULL, flprotect, szfile>>32, szfile&0xffffffff, C_NULL)
162172
if mmaphandle == C_NULL

0 commit comments

Comments
 (0)