diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/ComprehensionExpression.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/ComprehensionExpression.kt index 040f43cab6..ed36ae1982 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/ComprehensionExpression.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/ComprehensionExpression.kt @@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.graph.statements.expressions import de.fraunhofer.aisec.cpg.graph.AccessValues import de.fraunhofer.aisec.cpg.graph.ArgumentHolder +import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgeOf import de.fraunhofer.aisec.cpg.graph.edges.ast.astOptionalEdgeOf import de.fraunhofer.aisec.cpg.graph.edges.unwrapping import de.fraunhofer.aisec.cpg.graph.statements.Statement @@ -39,7 +40,8 @@ import org.neo4j.ogm.annotation.Relationship class ComprehensionExpression : Expression(), ArgumentHolder { @Relationship("VARIABLE") var variableEdge = - astOptionalEdgeOf( + astEdgeOf( + of = ProblemExpression("Missing variableEdge in ${this::class}"), onChanged = { _, new -> val end = new?.end if (end is Reference) { @@ -54,7 +56,9 @@ class ComprehensionExpression : Expression(), ArgumentHolder { */ var variable by unwrapping(ComprehensionExpression::variableEdge) - @Relationship("ITERABLE") var iterableEdge = astOptionalEdgeOf() + @Relationship("ITERABLE") + var iterableEdge = + astEdgeOf(ProblemExpression("Missing iterable in ${this::class}")) /** This field contains the iteration subject of the loop. */ var iterable by unwrapping(ComprehensionExpression::iterableEdge) 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 1aca6d4fa9..e9b06fc1ab 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 @@ -362,7 +362,6 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass state.push(currentNode, it) } } else if (currentNode is ComprehensionExpression) { - val iterable = currentNode.iterable as? Expression val writtenTo = when (val variable = currentNode.variable) { is DeclarationStatement -> { @@ -391,13 +390,11 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : EOGStarterPass } } - iterable?.let { - state.push(writtenTo, PowersetLattice(identitySetOf(iterable))) - // Add the variable declaration (or the reference) to the list of previous - // write nodes in this path - state.declarationsState[writtenDeclaration] = - PowersetLattice(identitySetOf(writtenTo)) - } + state.push(writtenTo, PowersetLattice(identitySetOf(currentNode.iterable))) + // Add the variable declaration (or the reference) to the list of previous + // write nodes in this path + state.declarationsState[writtenDeclaration] = + PowersetLattice(identitySetOf(writtenTo)) } } else if (currentNode is ForEachStatement && currentNode.variable != null) { // The VariableDeclaration in the ForEachStatement doesn't have an initializer, so diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt index 54b1362d03..73066ad7b5 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt @@ -166,10 +166,8 @@ class DFGPass(ctx: TranslationContext) : ComponentPass(ctx) { * predicate(s). */ protected fun handleComprehensionExpression(comprehension: ComprehensionExpression) { - comprehension.variable?.let { variable -> - comprehension.iterable?.let { iterable -> iterable.nextDFG += variable } - comprehension.prevDFG += variable - } + comprehension.iterable.nextDFG += comprehension.variable + comprehension.prevDFG += comprehension.variable comprehension.predicate?.let { comprehension.prevDFG += it } } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/EvaluationOrderGraphPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/EvaluationOrderGraphPass.kt index b7d29954ed..103a5ecc52 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/EvaluationOrderGraphPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/EvaluationOrderGraphPass.kt @@ -419,7 +419,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa // find out for java, but impossible for c++) // evaluate the call target first, optional base should be the callee or in its subtree - node.callee?.let { handleEOG(it) } + handleEOG(node.callee) // then the arguments for (arg in node.arguments) { @@ -862,7 +862,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa * Connects the current EOG leaf nodes to the last stacked node, e.g. loop head, and removes the * nodes. * - * @param loopScope the loop scope + * @param loopStatement the loop statement */ protected fun handleContainedBreaksAndContinues(loopStatement: LoopStatement) { // Breaks are connected to the NEXT EOG node and therefore temporarily stored after the loop @@ -894,11 +894,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa } /** - * Builds an EOG edge from prev to next. 'eogDirection' defines how the node instances save the - * references constituting the edge. 'FORWARD': only the nodes nextEOG member contains - * references, an points to the next nodes. 'BACKWARD': only the nodes prevEOG member contains - * references and points to the previous nodes. 'BIDIRECTIONAL': nextEOG and prevEOG contain - * references and point to the previous and the next nodes. + * Builds an EOG edge from prev to next. * * @param prev the previous node * @param next the next node @@ -952,8 +948,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa private fun handleComprehensionExpression(node: ComprehensionExpression) { handleEOG(node.iterable) // When the iterable contains another element, the variable is evaluated with the - // nextElement. Therefore we ad a - // true edge. + // nextElement. Therefore, we add a "true" edge. nextEdgeBranch = true handleEOG(node.variable) handleEOG(node.predicate) @@ -1233,7 +1228,7 @@ open class EvaluationOrderGraphPass(ctx: TranslationContext) : TranslationUnitPa else -> { LOGGER.error( "Currently the component {} does not have a defined loop start.", - this?.javaClass + this.javaClass ) ArrayList() }