Repository navigation
fix: do not recurse forever on recursively embedded structs - #1087
Merged
Merged
Conversation
pelletier
enabled auto-merge (squash)
July 3, 2026 02:23
pelletier
force-pushed
the
fix/unmarshal-recursive-embedded
branch
from
July 3, 2026 02:26
80e9ba0 to
e5cd44c
Compare
Unmarshaling into a struct type that embeds itself, directly (type A
struct { *A; ... }) or through another type, hung the decoder: addFields
flattens embedded structs by recursing on their type with no
visited-type guard, so a recursive embedding recursed forever at
plan-build time, regardless of the document.
Guard addFields with the same visited map the encoder's buildEncPlan
already uses: a type currently being flattened on this branch is not
descended into again. The skipped fields are unreachable by flattening
anyway, and sibling (non-cyclic) repeats of a type keep flattening as
before thanks to the enter/defer-delete pattern.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pelletier
force-pushed
the
fix/unmarshal-recursive-embedded
branch
from
July 3, 2026 02:35
e5cd44c to
81457d1
Compare
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.
What
Unmarshaling into a struct type that embeds itself β directly (
type A struct { *A; X int }) or mutually through another type β hung the decoder with unbounded memory growth.addFieldsflattens embedded structs by recursing on their type with no visited-type guard, so a recursive embedding recursed forever at plan-build time, regardless of the input document.addFieldsnow carries the samevisited map[reflect.Type]boolguard the encoder'sbuildEncPlanhas used all along: a type currently being flattened on this branch is not descended into again. The skipped fields are unreachable by flattening anyway, and the enter/defer-delete pattern keeps sibling (non-cyclic) repeats of a type flattening exactly as before.Marshalof the same types already worked (the nil embedded pointer stops the value walk), so this brings decode in line with encode.The hang is pre-existing, not a reimplementation regression: v2.3.1 hangs on the same input in its per-expression field walk.
Testing
TestUnmarshalRecursiveEmbedded(self-embedding, mutual embedding, and a same-type-twice-without-cycle control) β before the fix the test hangs until the go test timeout kills it with the recursion stack; green after.go test -race ./...green;golangci-lint(v2.8.0) clean;./caps.sh checkunchanged;addFields/buildPlanat 100% coverage, total unchanged at 97.2%.Benchmarks
The visited map is allocated once per plan build, which runs once per type per process (cached in
structPlans); the steady-state decode path is untouched. Interleaved A/B on linux/amd64 (go1.26.4): all decode benchmarks statistically unchanged (geomean +0.13% on a focused n=24 re-run; the few marshal deltas flagged in the first pass disappeared at higher power, consistent with layout noise β the change is unreachable from the marshal path and allocations are byte-identical).Out of scope
While writing the test I hit a separate pre-existing issue (reproduces on v2.4.2 without any recursion): decoding through a nil unexported embedded pointer (
type outer struct{ *inner }) panics withreflect: reflect.Value.Set using value obtained using unexported fieldinstead of returning an error. Worth its own issue; encoding/json reports a proper error for this shape.π€ Generated with Claude Code