Skip to content

Commit b3378f6

Browse files
authored
[kotlin2cpg] Fix NoSuchElementException in SAM interface handling. (#5671)
When only partial type information is available, the expected type of a lambda or function expression may not be available. This scenario was not correctly handled which led to the NoSuchElementException. Fix for https://shiftleftinc.atlassian.net/browse/SEN-4472
1 parent 23bb2a2 commit b3378f6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstForFunctionsCreator.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,11 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) {
528528
call.getResolvedCallAtom
529529
}
530530

531-
resolvedCallAtom.map { callAtom =>
531+
resolvedCallAtom.flatMap { callAtom =>
532532
callAtom.getCandidateDescriptor match {
533533
case samConstructorDesc: SamConstructorDescriptor =>
534534
// Lambda is wrapped e.g. `SomeInterface { obj -> obj }`
535-
samConstructorDesc.getBaseDescriptorForSynthetic
535+
Some(samConstructorDesc.getBaseDescriptorForSynthetic)
536536
case _ =>
537537
// Lambda/anon function is directly used as call argument e.g. `someCall(obj -> obj)`
538538
val samInterfaceViaConversion = callAtom.getArgumentsWithConversion.asScala.collectFirst {
@@ -541,9 +541,10 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) {
541541
.asInstanceOf[ClassDescriptor]
542542
}
543543

544-
val samInterface = samInterfaceViaConversion.getOrElse {
545-
val expectedExprType = bindingUtils.getExpectedExprType(expr).get
546-
expectedExprType.getConstructor.getDeclarationDescriptor.asInstanceOf[ClassDescriptor]
544+
val samInterface = samInterfaceViaConversion.orElse {
545+
bindingUtils.getExpectedExprType(expr).map { expectedExprType =>
546+
expectedExprType.getConstructor.getDeclarationDescriptor.asInstanceOf[ClassDescriptor]
547+
}
547548
}
548549

549550
samInterface

0 commit comments

Comments
 (0)