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

[Gosrc2cpg] - Limit literal code character length #3728

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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 @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading