Repository navigation
fix: handle interface destinations holding pointers or requiring methods - #153
Merged
Merged
Conversation
When the destination interface already held a pointer, the stream decoder read the next byte instead of reusing the code byte it had already read, failing with EOF or decoding the wrong value. A typed nil pointer held by the interface made the buffer decoder panic and the stream decoder fail. Decode into the held pointer only when it is non-nil, reusing the code byte in the stream decoder, and otherwise replace the interface value.
…tinations
Decoding into an interface with methods, such as error or fmt.Stringer,
set the decoded value with reflect.Set, which panics when the value does
not implement the interface. Check the value first and return an error
wrapping ErrCanNotDecode. interface{} destinations skip the check.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## v2 #153 +/- ##
==========================================
+ Coverage 97.84% 97.87% +0.03%
==========================================
Files 74 74
Lines 6019 6033 +14
==========================================
+ Hits 5889 5905 +16
+ Misses 74 73 -1
+ Partials 56 55 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
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.
Summary
error,fmt.Stringer, ...)Background
These bugs exist in v2 and v3. They only appear with these destination types:
UnmarshalReadread the next byte instead of reusing the code byte it had already read. That gave EOF, or the wrong value when more data followed.Unmarshalpanicked andUnmarshalReadreturned EOF.errorfield: both decoders panicked inreflect.Setwhen the value did not implement the interface. Round-tripping a struct with a non-nilerrorfield hits this.Changes
encoding/json.ErrCanNotDecodeif it does not.interface{}destinations skip the check.Test Plan
go vet ./...andgo test -race ./...[]interface{}/map[interface{}]interface{}cases, about 0.6ns per element, from the empty-interface type check.Notes
unsafe, and this library avoidsunsafe. Is the small per-element cost acceptable?