From 7cf5df80cee1734a752fe3b2d75cd671ea121b72 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Sun, 13 Mar 2022 15:36:15 +0530 Subject: [PATCH] Fix getParentSpaces can cause call stack overflow Signed-off-by: Ajay Bura --- src/client/state/RoomList.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/client/state/RoomList.js b/src/client/state/RoomList.js index 8f3233c..0153c45 100644 --- a/src/client/state/RoomList.js +++ b/src/client/state/RoomList.js @@ -92,13 +92,21 @@ class RoomList extends EventEmitter { } getParentSpaces(roomId) { - let parentIds = this.roomIdToParents.get(roomId); - if (parentIds) { - [...parentIds].forEach((parentId) => { - parentIds = new Set([...parentIds, ...this.getParentSpaces(parentId)]); - }); - } - return parentIds || new Set(); + const allParents = new Set(); + const processed = new Set(); + const addAllParentIds = (rId) => { + if (processed.has(rId)) return; + processed.add(rId); + + const parents = this.roomIdToParents.get(rId); + if (parents === undefined) return; + + parents.forEach((id) => allParents.add(id)); + parents.forEach((id) => addAllParentIds(id)); + }; + addAllParentIds(roomId); + processed.clear(); + return allParents; } addToSpaces(roomId) {