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") + } + } + } + }