From fc96dfce9b06712c8a638b89587a48487e5382a4 Mon Sep 17 00:00:00 2001 From: Mathias Morbitzer Date: Fri, 22 Dec 2023 13:07:43 +0100 Subject: [PATCH] fixed endless loop in PDG --- .../aisec/cpg/passes/ProgramDependenceGraphPass.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt index e83baaf431..e1418255e5 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ProgramDependenceGraphPass.kt @@ -97,8 +97,11 @@ class ProgramDependenceGraphPass(ctx: TranslationContext) : TranslationUnitPass( private fun allEOGsFromToFlowThrough(from: Node, to: Node, through: Node): Boolean { val worklist = mutableListOf(from) + val alreadySeenNodes = mutableSetOf() + while (worklist.isNotEmpty()) { val currentStatus = worklist.removeFirst() + alreadySeenNodes.add(currentStatus) val nextEOG = currentStatus.nextEOG.filter { it != through } if (nextEOG.isEmpty()) { // This path always flows through "through" or has not seen "to", so we're good @@ -108,13 +111,14 @@ class ProgramDependenceGraphPass(ctx: TranslationContext) : TranslationUnitPass( // path. return false } else { - if (nextEOG.size == 1) { + if (nextEOG.size == 1 && nextEOG.single() !in alreadySeenNodes) { worklist.add(nextEOG.single()) - } else if (nextEOG.isEmpty()) { - // Nothing to do. This path doesn't lead us to "to". - continue } else { - worklist.addAll(nextEOG) + for (next in nextEOG) { + if (next !in alreadySeenNodes) { + worklist.add(next) + } + } } } }