Repository navigation
fix: panic on cyclic aliases at registration instead of overflowing the stack - #1703
Open
draarivpatel-ui wants to merge 2 commits into
Open
draarivpatel-ui wants to merge 2 commits into
draarivpatel-ui wants to merge 2 commits into
Conversation
…he stack Registering aliases that form a cycle (loop -> loop, a -> b -> a, or longer chains) made parseFieldTagsRecursive recurse without end the first time the tag was used, killing the process with a fatal stack overflow that recover() cannot catch. RegisterAlias now walks the expansion of the alias being registered through the already registered aliases and panics when it can reach the alias again, so the misconfiguration is reported where it is introduced. This fits the documented intent that aliases are registered prior to any validation. Forward references to aliases registered later keep working. Fixes go-playground#1673
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
nodivbyzero
reviewed
Nov 26, 2026
| PanicMatches(t, func() { validate.RegisterAlias("exists!", "gt=5,lt=10") }, "Alias 'exists!' either contains restricted characters or is the same as a restricted tag needed for normal operation") | ||
| } | ||
|
|
||
| func TestCyclicAliasPanicsAtRegistration(t *testing.T) { |
Contributor
There was a problem hiding this comment.
How about this test case?
t.Run("alias name colliding with parameterized built-in", func(t *testing.T) {
validate := New()
// "len" is not a restricted alias name, so this is allowed today.
// But the cycle check splits "len=5" into name "len" == alias → panics
// with "Alias 'len' expands to itself...", even though at validation time
// "len=5" resolves to the built-in length validator, not the alias.
validate.RegisterAlias("len", "len=5")
Equal(t, validate.Var("12345", "len"), nil) // bare "len" uses the alias
Equal(t, validate.Var("12345", "len=5"), nil) // "len=5" uses the built-in
})
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.
Fixes #1673
Problem
Aliases that form a cycle make
parseFieldTagsRecursiverecurse without end:The first time the tag is used, the process dies with
fatal error: stack overflow. That is a fatal runtime error, sorecover()cannot catch it and the whole program goes down.Fix
RegisterAliasnow walks the expansion of the alias being registered through the already registered aliases and panics when it can reach the alias again:The misconfiguration is now reported where it is introduced, which fits the documented intent that aliases are registered prior to any validation. The panic uses the same mechanism as the existing restricted-name check in
RegisterAlias. Cycles are detected in every position an alias can expand from: full tag segments,|-separated alternatives, and tags with parameters.Forward references still work: registering
aas"b"beforebexists is fine, because the walk only follows aliases that are already registered. Oncebis registered, the check runs against the complete graph again.Testing
Added
TestCyclicAliasPanicsAtRegistrationcovering a self reference, a mutual pair, a three-alias chain, a cycle through an|alternative, and a positive test showing forward references and chains register cleanly and validate as expected. The cyclic cases fail on master (no panic at registration) and pass with the fix; the full test suite passes.