avoid infinite loop in Tangle.precedes()

This commit is contained in:
Andre Staltz 2024-01-17 15:47:58 +02:00
parent 001fcaa1ab
commit 17742f9ed2
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 8 additions and 2 deletions

View File

@ -296,13 +296,19 @@ class Tangle {
if (msgAID === msgBID) return false
if (msgBID === this.#rootID) return false
let toCheck = [msgBID]
const checked = new Set()
while (toCheck.length > 0) {
const checking = /** @type {string} */ (toCheck.shift())
checked.add(checking)
const prev = this.#prev.get(checking)
if (!prev) continue
if (prev.includes(msgAID)) return true
toCheck.push(...prev)
if (prev.includes(msgAID)) {
checked.clear()
return true
}
toCheck.push(...prev.filter((p) => !checked.has(p)))
}
checked.clear()
return false
}