Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safer serialization of ::SubArray{T,N,A<:Array} #14625

Merged
merged 1 commit into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Serializer

import Base: GMP, Bottom, svec, unsafe_convert, uncompressed_ast
using Base: ViewIndex, dimsize

export serialize, deserialize

Expand Down Expand Up @@ -191,15 +192,27 @@ function serialize(s::SerializationState, a::Array)
end

function serialize{T,N,A<:Array}(s::SerializationState, a::SubArray{T,N,A})
if !isbits(T) || stride(a,1)!=1
return serialize(s, copy(a))
end
writetag(s.io, ARRAY_TAG)
serialize(s, T)
serialize(s, size(a))
serialize_array_data(s.io, a)
b = trimmedsubarray(a)
serialize_any(s, b)
end

function trimmedsubarray{T,N,A<:Array}(V::SubArray{T,N,A})
dest = Array(eltype(V), trimmedsize(V))
copy!(dest, V)
_trimmedsubarray(dest, V, (), V.indexes...)
end

trimmedsize(V) = _trimmedsize(V, (), V.indexes...)
_trimmedsize(V, trimsz) = trimsz
_trimmedsize(V, trimsz, index, indexes...) = _trimmedsize(V, (trimsz..., dimsize(V.parent, length(trimsz)+1, index)), indexes...)

_trimmedsubarray{T,N,P,I,LD}(A, V::SubArray{T,N,P,I,LD}, newindexes) = SubArray{T,N,P,I,LD}(A, newindexes, size(V), 1, 1)
_trimmedsubarray(A, V, newindexes, index::ViewIndex, indexes...) = _trimmedsubarray(A, V, (newindexes..., trimmedindex(V.parent, length(newindexes)+1, index)), indexes...)

trimmedindex(P, d, i::Real) = oftype(i, 1)
trimmedindex(P, d, i::Colon) = i
trimmedindex(P, d, i::AbstractVector) = oftype(i, 1:length(i))

function serialize{T<:AbstractString}(s::SerializationState, ss::SubString{T})
# avoid saving a copy of the parent string, keeping the type of ss
serialize_any(s, convert(SubString{T}, convert(T,ss)))
Expand Down
24 changes: 24 additions & 0 deletions test/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ create_serialization_stream() do s # slices
@test deserialize(s) == slc2
end

# Objects that have a SubArray as a type in a type-parameter list
module ArrayWrappers

immutable ArrayWrapper{T,N,A<:AbstractArray} <: AbstractArray{T,N}
data::A
end
Base.size(A::ArrayWrapper) = size(A.data)
Base.size(A::ArrayWrapper, d) = size(A.data, d)
Base.getindex(A::ArrayWrapper, i::Real...) = getindex(A.data, i...)

end

let A = rand(3,4)
for B in (sub(A, :, 2:4), slice(A, 2, 1:3))
C = ArrayWrappers.ArrayWrapper{Float64,2,typeof(B)}(B)
io = IOBuffer()
serialize(io, C)
seek(io, 0)
Cd = deserialize(io)
@test size(Cd) == size(C)
@test Cd == B
end
end

# Function
serialize_test_function() = 1
serialize_test_function2 = ()->1
Expand Down