Skip to content

Commit a215709

Browse files
author
Tebogo Selahle
committed
fix: resolve multiple self identifier refOuts bug
1 parent a4d265d commit a215709

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

joern-cli/frontends/rubysrc2cpg/src/main/scala/io/joern/rubysrc2cpg/astcreation/AstForFunctionsCreator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
237237
case i: NewIdentifier if capturedNodes.map(_.name).contains(i.name) => i
238238
}
239239
// Copy AST block detaching the REF nodes between parent locals/params and identifiers, with the closures' one
240-
val capturedBlockAst = baseStmtBlockAst.copy(refEdges = baseStmtBlockAst.refEdges.filterNot {
240+
val capturedBlockAst = baseStmtBlockAst.copy(refEdges = baseStmtBlockAst.refEdges.filter {
241241
case AstEdge(_: NewIdentifier, dst: DeclarationNew) => capturedNodes.contains(dst)
242242
case _ => false
243243
})

joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/querying/DoBlockTests.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,26 @@ class DoBlockTests extends RubyCode2CpgFixture {
544544
backRefCall.lineNumber shouldBe Option(3)
545545
backRefCall.columnNumber shouldBe Option(29)
546546
}
547+
548+
"A `self` reference in a lambda" should {
549+
val cpg = code(
550+
"""
551+
|foo("something") { |x| send x }
552+
|""".stripMargin)
553+
554+
"not result in `self` identifiers with multiple refOut edges" in {
555+
inside(cpg.method.isLambda.ast.fieldAccess.argument.isIdentifier.distinct.nameExact(RubyDefines.Self).l) {
556+
case (identifier: Identifier) :: Nil =>
557+
inside(identifier.refOut.l) {
558+
case (local: Local) :: Nil =>
559+
local.name shouldBe "self"
560+
local.closureBindingId shouldBe Some("Test0.rb:<main>.<lambda>0.self")
561+
case xs =>
562+
fail(s"Expected a single local, got [${xs.code.mkString(",")}]")
563+
}
564+
case Nil =>
565+
fail(s"Expected `self` identifiers, got none")
566+
}
567+
}
568+
}
547569
}

0 commit comments

Comments
 (0)