Fix panic unmarshaling a child node that shares a parent property name - #14
Open
ChrisJr404 wants to merge 1 commit into
Open
ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
…y name
When a node has both a property and a child node that share the same name,
the map entry for that key already holds a concrete value (from the property)
by the time the child node is unmarshaled into an interface{}. Deriving the
target with reflect.Value.Elem() yields an unaddressable value, so the
single-argument and multi-argument interface branches panicked with
"reflect.Value.Set using unaddressable value".
Guard those Set calls with CanSet and fall back to assigning through the
dest pointer, matching the existing map branch. Adds a regression test.
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
Unmarshalpanics on certain malformed-but-parseable documents when a nodecarries both a property and a child node that share the same name.
Minimal reproducer:
This panics with:
Other value shapes for the child argument trigger it too (
1.5,true, ahex literal, multiple arguments), and it also reproduces with a well-formed
closing brace once the name collision is present.
Cause
In the
reflect.Interfacebranch ofunmarshalNodeToValue, the target valueis derived with:
When the map key already holds a concrete value (because the parent property
of the same name was unmarshaled first),
v.Elem()returns an unaddressablevalue. The single-argument and multi-argument sub-branches then call
v.Set(...)unconditionally, which panics. The third sub-branch (the map case) already
guards this with
v.CanSet()and falls back to writing through*dest.Fix
Apply the same
CanSet()guard to the single-argument and multi-argumentbranches, falling back to assigning through the
destpointer sowithCreatedAndIndirectedpropagates the result — matching the existing mapbranch. No change to the happy path.
Added
TestBug10covering the crashing inputs; it panics before the change andpasses after.