diff --git a/base/mmap.jl b/base/mmap.jl index 0477ea8482686..5f9cda44f0fe2 100644 --- a/base/mmap.jl +++ b/base/mmap.jl @@ -204,9 +204,11 @@ const MS_INVALIDATE = 2 const MS_SYNC = 4 function sync!{T}(m::Array{T}, flags::Integer=MS_SYNC) - @unix_only systemerror("msync", ccall(:msync, Cint, (Ptr{Void}, Csize_t, Cint), pointer(m), length(m)*sizeof(T), flags) != 0) + offset = rem(UInt(pointer(m)), PAGESIZE) + ptr = pointer(m) - offset + @unix_only systemerror("msync", ccall(:msync, Cint, (Ptr{Void}, Csize_t, Cint), ptr, length(m)*sizeof(T), flags) != 0) @windows_only systemerror("could not FlushViewOfFile: $(Libc.FormatMessage())", - ccall(:FlushViewOfFile, stdcall, Cint, (Ptr{Void}, Csize_t), pointer(m), length(m)) == 0) + ccall(:FlushViewOfFile, stdcall, Cint, (Ptr{Void}, Csize_t), ptr, length(m)) == 0) end sync!(B::BitArray, flags::Integer=MS_SYNC) = sync!(B.chunks, flags) diff --git a/test/mmap.jl b/test/mmap.jl index 0cbb56d3dad33..b378427890926 100644 --- a/test/mmap.jl +++ b/test/mmap.jl @@ -278,3 +278,16 @@ n = similar(m, 12) @test length(n) == 12 @test size(n) == (12,) finalize(m); m = nothing; gc() + +# test #14885 +file = tempname() +touch(file) +open(file, "r+") do s + A = Mmap.mmap(s, Vector{UInt8}, (10,), 0); + Mmap.sync!(A) + finalize(A); A = nothing; gc() + A = Mmap.mmap(s, Vector{UInt8}, (10,), 1); + Mmap.sync!(A) + finalize(A); A = nothing; gc() +end +rm(file)