From 28111101199c81a96e2e57007e84725dc736819e Mon Sep 17 00:00:00 2001 From: Ankit Kumar <118803988+ankit-privado@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:25:51 +0530 Subject: [PATCH] Using Block Scope fix (#109) * fix methodFullName inside using block * code refactoring * Try handling change * added curly braces --- .../astcreation/AstForStatementsCreator.scala | 7 ++-- .../querying/ast/ControlStructureTests.scala | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForStatementsCreator.scala b/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForStatementsCreator.scala index 2a12381aeee3..ab43bff6aa2d 100644 --- a/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForStatementsCreator.scala +++ b/joern-cli/frontends/csharpsrc2cpg/src/main/scala/io/joern/csharpsrc2cpg/astcreation/AstForStatementsCreator.scala @@ -300,10 +300,13 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t .code(code(usingStmt)) .lineNumber(line(usingStmt)) .columnNumber(column(usingStmt)) + val declAst = Try(createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))) match { + case Success(declNodevalue) => astForNode(declNodevalue) + case _ => Seq.empty[Ast] + } + val tryNodeInfo = createDotNetNodeInfo(usingStmt.json(ParserKeys.Statement)) val tryAst = astForBlock(tryNodeInfo, Option("try")) - val declNode = createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration)) - val declAst = astForNode(declNode) val finallyAst = declAst.flatMap(_.nodes).collectFirst { case x: NewIdentifier => x.copy }.map { id => val callCode = s"${id.name}.Dispose()" diff --git a/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/ControlStructureTests.scala b/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/ControlStructureTests.scala index f3f3d6d0180d..3a412f347602 100644 --- a/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/ControlStructureTests.scala +++ b/joern-cli/frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/ControlStructureTests.scala @@ -173,4 +173,37 @@ class ControlStructureTests extends CSharpCode2CpgFixture { } + "having using statement" should { + val cpg = code(""" + |namespace other + |{ + | public class General + | { + | public static void Call(string name) + | { + | using (SqlConnection connection = new SqlConnection(name)) + | { + | try + | { + | connection.Open(); + | } + | catch (Exception ex) + | { + | Console.WriteLine(ex.Message); + | connection.Close(); + | } + | } + | } + | } + |} + |""".stripMargin) + + "resolve methodFullName" in { + inside(cpg.call.name("Open").methodFullName.l) { + case x :: Nil => x shouldBe "SqlConnection.Open:" + case _ => fail("Unexpected call node structure") + } + } + } + }