Skip to content

Fix encoding self-referencing types - #833

Merged
fxamacker merged 1 commit into
release-2.9from
fxamacker/release-2.9-fix-encodiong-with-self-reference-type
Oct 1, 2026
Merged

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

Conversation

@fxamacker

@fxamacker fxamacker commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

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

This PR handles self-referencing types by storing newly created encoding functions in a temporary cache during type traversal. The temporary cache is created per getEncodeFunc() cache miss. When a type is encountered during traversal, the temporary cache is checked first, followed by the regular cache, and then getEncodeFuncInternal() is called. If a type is encountered recursively during traversal, a closure function that indirectly calls the type's encoding function is returned. 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
@fxamacker
fxamacker marked this pull request as draft September 24, 2026 02:51
@fxamacker
fxamacker force-pushed the fxamacker/release-2.9-fix-encodiong-with-self-reference-type branch 2 times, most recently from 3b02113 to e331380 Compare October 1, 2026 00:33
Not affected: user apps that don't encode a Go type that refers
to itself through a cycle containing an array, slice, or map
element without a struct in the cycle (e.g. type T map[string]T).

This commit handles self-referencing types by storing newly created
encoding functions in a temporary cache during type traversal.  The
temporary cache is created per getEncodeFunc() cache miss.  When a
type is encountered during traversal, the temporary cache is checked
first, followed by the regular cache, and then getEncodeFuncInternal()
is called.  If a type is encountered recursively during traversal, a
closure function that indirectly calls the type's encoding function is
returned.  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 force-pushed the fxamacker/release-2.9-fix-encodiong-with-self-reference-type branch from e331380 to 3461c94 Compare October 1, 2026 01:09
@fxamacker
fxamacker marked this pull request as ready for review October 1, 2026 13:17
@fxamacker
fxamacker merged commit 2d4e13c into release-2.9 Oct 1, 2026
25 checks passed
@fxamacker
fxamacker deleted the fxamacker/release-2.9-fix-encodiong-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