Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSharp] : Increase robustness - Block AST #5284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.shiftleft.codepropertygraph.generated.nodes.{NewFieldIdentifier, NewId
import io.shiftleft.codepropertygraph.generated.{ControlStructureTypes, DispatchTypes, ModifierTypes, Operators}

import scala.::
import scala.util.Try
import scala.util.{Try, Success, Failure}

trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>

Expand Down Expand Up @@ -362,11 +362,13 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
* Thus, this is lowered as a try-finally, with finally making a call to `Dispose` on the declared variable.
*/
private def astForUsingStatement(usingStmt: DotNetNodeInfo): Seq[Ast] = {
val tryNode = controlStructureNode(usingStmt, ControlStructureTypes.TRY, code(usingStmt))
val tryNode = controlStructureNode(usingStmt, ControlStructureTypes.TRY, code(usingStmt))
val declAst = Try(createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))) match {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we throw away the exception, rather lets do something like

Suggested change
val declAst = Try(createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))) match {
val declAst = Try(createDotNetNodeInfo(usingStmt.json(ParserKeys.Declaration))).map(astForNode).getOrElse(Seq.empty[Ast])

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()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,37 @@ class ControlStructureTests extends CSharpCode2CpgFixture {

}

"having using statement" should {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"having using statement" should {
"a variable defined within a 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"resolve methodFullName" in {
"partially resolve calls on the defined variable" in {

inside(cpg.call.name("Open").methodFullName.l) {
case x :: Nil => x shouldBe "SqlConnection.Open:<unresolvedSignature>"
case _ => fail("Unexpected call node structure")
}
}
}

}
Loading