From e9ecdac226d46cf64d3f3e7c798579f6425e0d1b Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Tue, 19 Mar 2024 14:48:33 +0100 Subject: [PATCH] Less C&P --- .../cpg/passes/ControlFlowSensitiveDFGPass.kt | 50 ++++++------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlFlowSensitiveDFGPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlFlowSensitiveDFGPass.kt index cd628969aa3..c99894be79a 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlFlowSensitiveDFGPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/ControlFlowSensitiveDFGPass.kt @@ -139,6 +139,16 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass } } + /** + * Checks if there's an entry in [edgePropertiesMap] with key `(x, null)` where `x` is in [from] + * and, if so, adds an entry with key `(x, to)` and the same value + */ + protected fun findAndSetProperties(from: Set, to: Node) { + edgePropertiesMap + .filter { it.key.first in from && it.key.second == null } + .forEach { edgePropertiesMap[Pair(it.key.first, to)] = it.value } + } + /** * Removes all the incoming and outgoing DFG edges for each variable declaration in the block of * code [node]. @@ -230,13 +240,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass // We check if we have something relevant for this node (because there was an // entry for the incoming edge) in the edgePropertiesMap and, if so, we generate // a dedicated entry for the edge between declState and currentNode. - val relevantProperties = - edgePropertiesMap.filter { - it.key.first in declState.elements && it.key.second == null - } - relevantProperties.forEach { - edgePropertiesMap[Pair(it.key.first, currentNode)] = it.value - } + findAndSetProperties(declState.elements, currentNode) state.push(currentNode, declState) } else { // If we do not have a stored state of our object+field, we can use the field @@ -260,13 +264,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass // We check if we have something relevant for this node (because there was an // entry for the incoming edge) in the edgePropertiesMap and, if so, we generate // a dedicated entry for the edge between declState and currentNode. - val relevantProperties = - edgePropertiesMap.filter { - it.key.first in declState.elements && it.key.second == null - } - relevantProperties.forEach { - edgePropertiesMap[Pair(it.key.first, currentNode)] = it.value - } + findAndSetProperties(declState.elements, currentNode) state.push(currentNode, declState) } else { // If we do not have a stored state of our object+field, we can use the field @@ -311,13 +309,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass // We check if we have something relevant for this node (because there was an entry // for the incoming edge) in the edgePropertiesMap and, if so, we generate a // dedicated entry for the edge between declState and currentNode. - val relevantProperties = - edgePropertiesMap.filter { - it.key.first in (prev?.elements ?: setOf()) && it.key.second == null - } - relevantProperties.forEach { - edgePropertiesMap[Pair(it.key.first, currentNode)] = it.value - } + findAndSetProperties(prev?.elements ?: setOf(), currentNode) state.push(input, prev) doubleState.declarationsState[writtenDeclaration] = PowersetLattice(identitySetOf(input)) @@ -332,13 +324,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass if (writtenDeclaration != null && lhs != null) { val prev = doubleState.declarationsState[writtenDeclaration] - val relevantProperties = - edgePropertiesMap.filter { - it.key.first in (prev?.elements ?: setOf()) && it.key.second == null - } - relevantProperties.forEach { - edgePropertiesMap[Pair(it.key.first, currentNode)] = it.value - } + findAndSetProperties(prev?.elements ?: setOf(), currentNode) // Data flows from the last writes to the lhs variable to this node state.push(lhs, prev) @@ -359,13 +345,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass // We check if we have something relevant for this node (because there was an entry // for the incoming edge) in the edgePropertiesMap and, if so, we generate a // dedicated entry for the edge between declState and currentNode. - val relevantProperties = - edgePropertiesMap.filter { it2 -> - it2.key.first in it.elements && it2.key.second == null - } - relevantProperties.forEach { - edgePropertiesMap[Pair(it.key.first, currentNode)] = it.value - } + findAndSetProperties(it.elements, currentNode) state.push(currentNode, it) } } else if (