From 04ecc8575b87846da07e27a530ae70b6b2539dc8 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Tue, 13 Jun 2017 20:27:37 +0200 Subject: [PATCH] Prevent crashes from invalid id's in `Clustering.findNode()` (#3166) * Prevent crashes from invalid id's in `Clustering.findNode()` Fix for #3163 - Added safeguards in said method, to prevent exceptions happening when invalid id's are passed in. - Adjusted documentation for said method - Added notes to flag unused methods in `Clustering.js` * Removed incorrect NOTE-comments from methods that are indeed used --- docs/network/index.html | 11 ++++++++--- lib/network/modules/Clustering.js | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/network/index.html b/docs/network/index.html index c98ced3a..9bdf520a 100644 --- a/docs/network/index.html +++ b/docs/network/index.html @@ -611,13 +611,18 @@ var locales = { findNode( - String nodeId) + String/Number nodeId) Returns: Array Nodes can be in clusters. Clusters can also be in clusters. This function returns and array of - nodeIds - showing where the node is.

Example: + nodeIds showing where the node is. + +

+ If any nodeId in the chain, especially the first passed in as a parameter, is not present in + the current nodes list, an empty array is returned. + +

Example: cluster 'A' contains cluster 'B', cluster 'B' contains cluster 'C', cluster 'C' contains node 'fred'. diff --git a/lib/network/modules/Clustering.js b/lib/network/modules/Clustering.js index b039a0fc..f295c162 100644 --- a/lib/network/modules/Clustering.js +++ b/lib/network/modules/Clustering.js @@ -689,22 +689,32 @@ class ClusterEngine { /** * Get the stack clusterId's that a certain node resides in. cluster A -> cluster B -> cluster C -> node - * @param nodeId + * + * If a node can't be found in the chain, return an empty array. + * + * @param {string|number} nodeId * @returns {Array} */ findNode(nodeId) { let stack = []; let max = 100; let counter = 0; + let node; while (this.clusteredNodes[nodeId] !== undefined && counter < max) { - stack.push(this.body.nodes[nodeId].id); + node = this.body.nodes[nodeId] + if (node === undefined) return []; + stack.push(node.id); + nodeId = this.clusteredNodes[nodeId].clusterId; counter++; } - stack.push(this.body.nodes[nodeId].id); - stack.reverse(); + node = this.body.nodes[nodeId] + if (node === undefined) return []; + stack.push(node.id); + + stack.reverse(); return stack; }