Skip to content

Fix decoding to self-referencing types - #831

Merged
fxamacker merged 1 commit into
release-2.9from
fxamacker/release-2.9-fix-decoding-with-self-reference-type
Sep 23, 2026
Merged

fxamacker merged 1 commit into
release-2.9from
fxamacker/release-2.9-fix-decoding-with-self-reference-type

Conversation

@fxamacker

Copy link
Copy Markdown
Owner

Not affected: user apps that don't decode to a Go type that refers to itself through an array, slice, map, or pointer element without a struct in the cycle.

This PR handles self-referencing types by storing newly created typeInfo in a temporary cache during type traversal. The temporary cache is created per getTypeInfo() cache miss. When a type is encountered during traversal, the temporary cache is checked first, followed by the regular cache, and then newTypeInfo() is called. After traversal is completed, all types in the temporary cache are added to the regular cache.

Previously, traversing self-referencing types resulted in an unrecoverable stack overflow.

Not affected: user apps that don't decode to a Go type that refers
to itself through an array, slice, map, or pointer element without
a struct in the cycle (e.g. type T map[string]T).

This commit handles self-referencing types by storing newly created
typeInfo in a temporary cache during type traversal.  The temporary
cache is created per getTypeInfo() cache miss.  When a type is
encountered during traversal, the temporary cache is checked first,
followed by the regular cache, and then newTypeInfo() is called.
After traversal is completed, all types in the temporary cache are
added to the regular cache.

Previously, traversing self-referencing types resulted in an
unrecoverable stack overflow.
@fxamacker fxamacker self-assigned this Sep 23, 2026

@x448 x448 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks good. I didn't review the tests.

@fxamacker
fxamacker merged commit 2e08f36 into release-2.9 Sep 23, 2026
25 checks passed
@fxamacker
fxamacker deleted the fxamacker/release-2.9-fix-decoding-with-self-reference-type branch October 1, 2026 23:32
fxamacker added a commit that referenced this pull request Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants