From 222d93ca61513dd48cdfbb7878cc2831d07ac5a0 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Sat, 26 Oct 2024 10:22:34 +0300 Subject: [PATCH] fix reset.go Signed-off-by: Suleiman Dibirov --- loader/reset.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/loader/reset.go b/loader/reset.go index 467522ba..213d0e8f 100644 --- a/loader/reset.go +++ b/loader/reset.go @@ -28,7 +28,7 @@ import ( type ResetProcessor struct { target interface{} paths []tree.Path - visitedNodes map[*yaml.Node]bool + visitedNodes map[*yaml.Node]string } // UnmarshalYAML implement yaml.Unmarshaler @@ -43,23 +43,27 @@ func (p *ResetProcessor) UnmarshalYAML(value *yaml.Node) error { // resolveReset detects `!reset` tag being set on yaml nodes and record position in the yaml tree func (p *ResetProcessor) resolveReset(node *yaml.Node, path tree.Path) (*yaml.Node, error) { + pathStr := path.String() // If the path contains "<<", removing the "<<" element and merging the path - if strings.Contains(path.String(), ".<<") { - path = tree.NewPath(strings.Replace(path.String(), ".<<", "", 1)) + if strings.Contains(pathStr, ".<<") { + path = tree.NewPath(strings.Replace(pathStr, ".<<", "", 1)) } // Check for cycle if p.visitedNodes == nil { - p.visitedNodes = make(map[*yaml.Node]bool) + p.visitedNodes = make(map[*yaml.Node]string) } - // Check if the current node has been visited before (cycle detection) - if p.visitedNodes[node] { - return nil, fmt.Errorf("cycle detected at path: %s", path.String()) + // Check for cycle by seeing if the node has already been visited at this path + if previousPath, found := p.visitedNodes[node]; found { + // If the current node has been visited, we have a cycle if the previous path is a prefix + if strings.HasPrefix(pathStr, previousPath) { + return nil, fmt.Errorf("cycle detected at path: %s", pathStr) + } } // Mark the current node as visited - p.visitedNodes[node] = true + p.visitedNodes[node] = pathStr // If the node is an alias, We need to process the alias field in order to consider the !override and !reset tags if node.Kind == yaml.AliasNode {