fix: detect circular references in GetDescendants (#113672)

* fix: detect circular references in GetDescendants

* chore: use map[string]bool and instantiate at the beginning of the function
This commit is contained in:
Rafael Bortolon Paulovic
2025-11-11 12:46:35 +01:00
committed by GitHub
parent f34f7579a2
commit 194f7cb4f0
2 changed files with 297 additions and 5 deletions
+24 -5
View File
@@ -439,7 +439,10 @@ func (ss *FolderUnifiedStoreImpl) GetDescendants(ctx context.Context, orgID int6
}
descendantsMap := map[string]*folder.Folder{}
getDescendants(nodes, tree, ancestor_uid, descendantsMap)
err = getDescendants(nodes, tree, ancestor_uid, descendantsMap, nil)
if err != nil {
return nil, err
}
descendants := []*folder.Folder{}
for _, f := range descendantsMap {
@@ -449,11 +452,27 @@ func (ss *FolderUnifiedStoreImpl) GetDescendants(ctx context.Context, orgID int6
return descendants, nil
}
func getDescendants(nodes map[string]*folder.Folder, tree map[string]map[string]*folder.Folder, ancestor_uid string, descendantsMap map[string]*folder.Folder) {
for uid := range tree[ancestor_uid] {
descendantsMap[uid] = nodes[uid]
getDescendants(nodes, tree, uid, descendantsMap)
func getDescendants(
nodes map[string]*folder.Folder,
tree map[string]map[string]*folder.Folder,
ancestorUID string,
descendantsMap map[string]*folder.Folder,
seen map[string]bool,
) error {
if seen == nil {
seen = map[string]bool{}
}
if seen[ancestorUID] {
return folder.ErrCircularReference.Errorf("circular reference detected at folder uid: %s", ancestorUID)
}
seen[ancestorUID] = true
for uid := range tree[ancestorUID] {
descendantsMap[uid] = nodes[uid]
if err := getDescendants(nodes, tree, uid, descendantsMap, seen); err != nil {
return err
}
}
return nil
}
func (ss *FolderUnifiedStoreImpl) CountFolderContent(ctx context.Context, orgID int64, ancestor_uid string) (folder.DescendantCounts, error) {