Skip to content

Commit 76642c7

Browse files
authored
[semanticcpg] Catch and warn about failed method expansion. (#5891)
Some frontends generate invalid CPGs which lets the CFG node to METHOD expansion fail which results in an exception. In the passed we already handled this by returning null instead. We now return to this because using code checks for null in relevant places but does not handle exceptions and we do not feel comfortable releasing this without observing the logs for a while longer. There is also at least c2cpg which has a known CPG format issue for which we cannot release a fix to production. So this handling here will stay for a while.
1 parent 02ad1c1 commit 76642c7

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

semanticcpg/src/main/scala/io/shiftleft/semanticcpg/language/nodemethods/CfgNodeMethods.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ package io.shiftleft.semanticcpg.language.nodemethods
33
import io.shiftleft.codepropertygraph.generated.nodes.*
44
import io.shiftleft.semanticcpg.NodeExtension
55
import io.shiftleft.semanticcpg.language.*
6+
import org.slf4j.LoggerFactory
67

78
import scala.jdk.CollectionConverters.*
9+
import scala.util.Try
10+
11+
object CfgNodeMethods {
12+
private val logger = LoggerFactory.getLogger(classOf[CfgNodeMethods])
13+
}
814

915
class CfgNodeMethods(val node: CfgNode) extends AnyVal with NodeExtension {
1016
import CfgNodeMethods.*
@@ -107,8 +113,16 @@ class CfgNodeMethods(val node: CfgNode) extends AnyVal with NodeExtension {
107113
case callRepr: CallRepr if !callRepr.isInstanceOf[Call] => walkUpAst(callRepr)
108114
case annotation: Annotation => methodFromAnnotation(annotation)
109115
case annotationLiteral: AnnotationLiteral => methodFromAnnotation(annotationLiteral)
110-
case expr: Expression => methodViaContainsIn(expr)
111-
case jumpTarget: JumpTarget => methodViaContainsIn(jumpTarget)
116+
case expr: Expression =>
117+
Try(methodViaContainsIn(expr)).recover { exception =>
118+
logger.info("Unable to expand to method from expr {}. Exception: {}", expr.code, exception)
119+
null
120+
}.get
121+
case jumpTarget: JumpTarget =>
122+
Try(methodViaContainsIn(jumpTarget)).recover { exception =>
123+
logger.info("Unable to expand to method from jumpTarget {}. Exception: {}", jumpTarget.code, exception)
124+
null
125+
}.get
112126
}
113127

114128
private def methodViaContainsIn(node: CfgNode): Method = {

0 commit comments

Comments
 (0)