Skip to content

fix: do not recurse forever on recursively embedded structs - #1087

Merged
pelletier merged 1 commit into
v2from
fix/unmarshal-recursive-embedded
Jul 3, 2026
Merged

pelletier merged 1 commit into
v2from
fix/unmarshal-recursive-embedded

Conversation

@pelletier

@pelletier pelletier commented Jul 3, 2026 •

Copy link
Copy Markdown
Owner

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. 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 input document.

addFields now carries the same visited map[reflect.Type]bool guard the encoder's buildEncPlan has 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. Marshal of 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

  • Added regression test 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 check unchanged; addFields/buildPlan at 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 with reflect: reflect.Value.Set using value obtained using unexported field instead of returning an error. Worth its own issue; encoding/json reports a proper error for this shape.

πŸ€– Generated with Claude Code

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Jul 3, 2026
@pelletier
pelletier enabled auto-merge (squash) July 3, 2026 02:23
@pelletier
pelletier force-pushed the fix/unmarshal-recursive-embedded branch from 80e9ba0 to e5cd44c Compare July 3, 2026 02:26
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
pelletier force-pushed the fix/unmarshal-recursive-embedded branch from e5cd44c to 81457d1 Compare July 3, 2026 02:35
@pelletier
pelletier merged commit 6fa69af into v2 Jul 3, 2026
13 checks passed
@pelletier
pelletier deleted the fix/unmarshal-recursive-embedded branch July 3, 2026 02:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues describing a bug in go-toml.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant