fix: encode non-addressable values with pointer receiver methods#388
Open
Yanhu007 wants to merge 1 commit into
Open
fix: encode non-addressable values with pointer receiver methods#388Yanhu007 wants to merge 1 commit into
Yanhu007 wants to merge 1 commit into
Conversation
When encoding a value type that implements CustomEncoder, encoding.BinaryMarshaler, or encoding.TextMarshaler via pointer receivers, the encoder returns an unhelpful error if the value is not addressable (e.g. returned directly from a function or stored in an interface). Instead of returning an error, allocate a temporary addressable copy of the value and encode through that. This matches how encoding/json handles the same situation. Fixes vmihailenco#374
|
Hi @Yanhu007, solid fix. This repo has been unmaintained for ~3 years so PRs have been sitting without review. We maintain an active fork at github.com/Basekick-Labs/msgpack (v6) where we've already addressed this via an if v.CanAddr() {
return v.Addr()
}
// allocate addressable copy and set
vp := reflect.New(v.Type())
vp.Elem().Set(v)
return vpIf you're looking for an actively maintained home for this work, PRs to the fork are welcome. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #374
Problem
When encoding a value type that implements
CustomEncoder,encoding.BinaryMarshaler, orencoding.TextMarshalervia pointer receivers, the encoder returns:This happens because the encoder calls
v.Addr()which requires the value to be addressable — but values returned from functions, stored in interfaces, or used directly (not via pointer) are not addressable.Fix
In all four
*Addrencoder functions, whenv.CanAddr()is false, allocate a temporary addressable copy viareflect.New+Set, then proceed normally. This matches howencoding/jsonhandles the same situation.Testing
All existing tests pass.