From 1435b2373582b5a81747283cc87abdfd097d562b Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Tue, 10 Oct 2023 15:44:26 +0530 Subject: [PATCH] handled literal code character size --- .../astcreation/AstForPrimitivesCreator.scala | 3 +- .../go2cpg/passes/ast/LiteralCpgTests.scala | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/LiteralCpgTests.scala diff --git a/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/AstForPrimitivesCreator.scala b/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/AstForPrimitivesCreator.scala index c6fccbd21df5..67c81ee33c21 100644 --- a/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/AstForPrimitivesCreator.scala +++ b/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/AstForPrimitivesCreator.scala @@ -54,10 +54,9 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t } private def astForLiteral(stringLiteral: ParserNodeInfo): Ast = { - val code = stringLiteral.json(ParserKeys.Value).str // TODO May need to revisit this val typ = getTypeOfToken(stringLiteral) - Ast(literalNode(stringLiteral, code, typ)) + Ast(literalNode(stringLiteral, stringLiteral.code, typ)) } private def astForIdentifier(ident: ParserNodeInfo): Ast = { diff --git a/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/LiteralCpgTests.scala b/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/LiteralCpgTests.scala new file mode 100644 index 000000000000..dcc5aebb56c0 --- /dev/null +++ b/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/LiteralCpgTests.scala @@ -0,0 +1,45 @@ +package io.joern.go2cpg.passes.ast + +import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite +import io.shiftleft.codepropertygraph.generated.Operators +import io.shiftleft.semanticcpg.language.* + +import scala.util.Random + +class LiteralCpgTests extends GoCodeToCpgSuite { + "be correct for literal code having less than 1000 characters" in { + val cpg = code(s""" + |package main + |func foo() { + | var value = "${Random.alphanumeric.take(500).mkString}" + | } + |""".stripMargin) + + val List(callNode) = cpg.call.l + callNode.methodFullName shouldBe Operators.assignment + callNode.typeFullName shouldBe "string" + + val List(literalNode) = cpg.literal.l + literalNode.typeFullName shouldBe "string" + literalNode.code.size shouldBe 502 + literalNode.code.endsWith("...") shouldBe false + } + + "be correct for literal code having more than 1000 characters" in { + val cpg = code(s""" + |package main + |func foo() { + | var value = "${Random.alphanumeric.take(1001).mkString}" + | } + |""".stripMargin) + + val List(callNode) = cpg.call.l + callNode.methodFullName shouldBe Operators.assignment + callNode.typeFullName shouldBe "string" + + val List(literalNode) = cpg.literal.l + literalNode.typeFullName shouldBe "string" + literalNode.code.size shouldBe 1000 + literalNode.code.endsWith("...") shouldBe true + } +}