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

Julia: serialize the length of the model #2970

Merged
merged 2 commits into from Jun 9, 2021

Conversation

rcurtin
Copy link
Member

@rcurtin rcurtin commented Jun 7, 2021

While playing with the mlpack Julia bindings, I noticed that there is a bug in serialization that only happens in some niche cases.

Specifically, we serialize and deserialize the model directly to an IO stream, but we don't take the length of the serialized model into account. This means that at deserialization time, we eat everything in the stream---this screws up deserialization of anything that comes after the mlpack model.

Here's an example of a badly-behaved deserialization function:

function deserializeDecisionTreeModel(stream::IO)::DecisionTreeModel
          buffer = read(stream)
          DecisionTreeModel(ccall((:DeserializeDecisionTreeModelPtr, decision_treeLibrary), Ptr{Nothing}, (Ptr{UInt8}, UInt), Base.pointer(buffer), length(buffer)))
end

The solution here is, luckily, pretty easy: we just also serialize a UInt containing the length of the serialized model, and then deserialize that first to limit our read() to the appropriate length:

function deserializeDecisionTreeModel(stream::IO)::DecisionTreeModel
          buf_len = read(stream, UInt)
          buffer = read(stream, buf_len)
          DecisionTreeModel(ccall((:DeserializeDecisionTreeModelPtr, decision_treeLibrary), Ptr{Nothing}, (Ptr{UInt8}, UInt), Base.pointer(buffer), length(buffer)))
end

I added a test case for this situation also.

Copy link
Member

@zoq zoq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link

@mlpack-bot mlpack-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second approval provided automatically after 24 hours. 👍

@zoq zoq merged commit 66eecdc into mlpack:master Jun 9, 2021
This was referenced Oct 14, 2022
@rcurtin rcurtin mentioned this pull request Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants