diff --git a/cpg-analysis/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/UnreachableEOGPass.kt b/cpg-analysis/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/UnreachableEOGPass.kt index 1c87e6613f..01ab3025e4 100644 --- a/cpg-analysis/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/UnreachableEOGPass.kt +++ b/cpg-analysis/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/UnreachableEOGPass.kt @@ -124,15 +124,17 @@ private fun handleIfStatement( val evalResult = ValueEvaluator().evaluate(n.condition) val (unreachableEdge, remainingEdges) = - if (evalResult is Boolean && evalResult == true) { + if (evalResult == true) { + // If the condition is always true, the "false" branch is always unreachable Pair( - n.nextEOGEdges.firstOrNull { e -> e.index == 1 }, - n.nextEOGEdges.filter { e -> e.index != 1 } + n.nextEOGEdges.firstOrNull { e -> e.branch == false }, + n.nextEOGEdges.filter { e -> e.branch != false } ) - } else if (evalResult is Boolean && evalResult == false) { + } else if (evalResult == false) { + // If the condition is always false, the "true" branch is always unreachable Pair( - n.nextEOGEdges.firstOrNull { e -> e.index == 0 }, - n.nextEOGEdges.filter { e -> e.index != 0 } + n.nextEOGEdges.firstOrNull { e -> e.branch == true }, + n.nextEOGEdges.filter { e -> e.branch != true } ) } else { Pair(null, n.nextEOGEdges) diff --git a/cpg-language-java/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/EOGTest.kt b/cpg-language-java/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/EOGTest.kt index 08042255dd..49333511b7 100644 --- a/cpg-language-java/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/EOGTest.kt +++ b/cpg-language-java/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/EOGTest.kt @@ -110,7 +110,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = ifSimple.thenStatement, cr = Connect.NODE, - branch = true, + predicate = { it.branch == true }, refs = listOf(ifSimple) ) ) @@ -129,7 +129,7 @@ internal class EOGTest : BaseTest() { cn = Connect.NODE, en = Util.Edge.EXITS, n = ifSimple, - branch = true, + predicate = { it.branch == true }, refs = listOf(ifSimple.thenStatement) ) ) @@ -161,7 +161,7 @@ internal class EOGTest : BaseTest() { cn = Connect.NODE, en = Util.Edge.EXITS, n = ifBranched, - branch = true, + predicate = { it.branch == true }, refs = listOf(ifBranched.thenStatement) ) ) @@ -170,7 +170,7 @@ internal class EOGTest : BaseTest() { cn = Connect.NODE, en = Util.Edge.EXITS, n = ifBranched, - branch = false, + predicate = { it.branch == false }, refs = listOf(ifBranched.elseStatement) ) ) @@ -183,7 +183,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = ifBranched.thenStatement, cr = Connect.NODE, - branch = true, + predicate = { it.branch == true }, refs = listOf(ifBranched) ) ) @@ -194,7 +194,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = ifBranched.elseStatement, cr = Connect.NODE, - branch = false, + predicate = { it.branch == false }, refs = listOf(ifBranched) ) ) @@ -236,7 +236,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = bo.rhs, cr = Connect.SUBTREE, - branch = (bo.operatorCode == "&&"), + predicate = { it.branch == (bo.operatorCode == "&&") }, refs = listOf(bo.lhs) ) ) @@ -267,7 +267,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = bo, cr = Connect.SUBTREE, - branch = (bo.operatorCode != "&&"), + predicate = { it.branch == (bo.operatorCode != "&&") }, refs = listOf(bo.lhs) ) ) @@ -398,7 +398,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.EXITS, n = fs, cr = Connect.SUBTREE, - branch = false, + predicate = { it.branch == false }, refs = listOf(prints[2]) ) ) @@ -471,7 +471,7 @@ internal class EOGTest : BaseTest() { en = Util.Edge.ENTRIES, n = wstat.statement, cr = Connect.NODE, - branch = true, + predicate = { it.branch == true }, refs = listOf(wstat) ) ) @@ -483,7 +483,7 @@ internal class EOGTest : BaseTest() { cn = Connect.SUBTREE, en = Util.Edge.EXITS, n = wstat, - branch = false, + predicate = { it.branch == false }, refs = listOf(prints[1]) ) ) @@ -516,7 +516,7 @@ internal class EOGTest : BaseTest() { cn = Connect.NODE, en = Util.Edge.EXITS, n = dostat, - branch = true, + predicate = { it.branch == true }, refs = listOf(dostat.statement) ) ) @@ -537,7 +537,7 @@ internal class EOGTest : BaseTest() { cn = Connect.SUBTREE, en = Util.Edge.EXITS, n = dostat, - branch = false, + predicate = { it.branch == false }, refs = listOf(prints[2]) ) )