Skip to content

fix: panic on cyclic aliases at registration instead of overflowing the stack - #1703

Open
draarivpatel-ui wants to merge 2 commits into
go-playground:masterfrom
draarivpatel-ui:fix-cyclic-alias-panic
Open

draarivpatel-ui wants to merge 2 commits into
go-playground:masterfrom
draarivpatel-ui:fix-cyclic-alias-panic

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #1673

Problem

Aliases that form a cycle make parseFieldTagsRecursive recurse without end:

v := validator.New()
v.RegisterAlias("loop", "loop")
// or: v.RegisterAlias("a", "b"); v.RegisterAlias("b", "a")
// or longer chains through several aliases

The first time the tag is used, the process dies with fatal error: stack overflow. That is a fatal runtime error, so recover() cannot catch it and the whole program goes down.

Fix

RegisterAlias now walks the expansion of the alias being registered through the already registered aliases and panics when it can reach the alias again:

Alias 'loop' expands to itself, either directly or through other aliases

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 a as "b" before b exists is fine, because the walk only follows aliases that are already registered. Once b is registered, the check runs against the complete graph again.

Testing

Added TestCyclicAliasPanicsAtRegistration covering 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.

…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
@draarivpatel-ui
draarivpatel-ui requested a review from a team as a code owner November 25, 2026 16:39
Copilot AI balanced review requested due to automatic review settings November 25, 2026 16:39
@coveralls

coveralls commented Nov 25, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 72.69% (+0.02%) from 72.668% — draarivpatel-ui:fix-cyclic-alias-panic into go-playground:master

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread validator_test.go
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) {

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.

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
	})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Cyclic RegisterAlias causes a fatal stack overflow on first use

4 participants