Repository: joernio/joern (scala)

Issue:
Missing size of char array in CPG
For the given code we are missing the size (256) of the array, by importing it with the c frontend
```
int main() {
  char buf[256];
  printf("%s", buf);
}
```

See joern queries:
```
joern> importCode.c("/tmp/test/")
Creating project `test` for code at `/tmp/test/`
[...]
res6: Option[io.shiftleft.codepropertygraph.Cpg] = Some(
  value = io.shiftleft.codepropertygraph.Cpg@238869e8
)

joern> cpg.local.l
res7: List[Local] = List(
  Local(
    id -> 3074457345618258676L,
    closureBindingId -> None,
    code -> "buf",
    columnNumber -> None,
    dynamicTypeHintFullName -> ArraySeq(),
    lineNumber -> None,
    name -> "buf",
    order -> 1,
    typeFullName -> "char[]"
  )
)
```


I was not able to find the value after dumping the code (`cpg.all.p |> "/tmp/all.txt"`) too.

Pull request:
Fixes: https://github.com/ShiftLeftSecurity/codepropertygraph/issues/1526

Patch:
diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
index a96a5ad3c1f8..fef4f42cdf24 100644
--- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
+++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala
@@ -121,9 +121,7 @@ trait AstCreatorHelper {
 
   @nowarn
   protected def typeFor(node: IASTNode): String = node match {
-    case _: IASTIdExpression =>
-      cleanType(ASTTypeUtil.getNodeType(node))
-    case _: IASTName =>
+    case _: IASTIdExpression | _: IASTName | _: IASTArrayDeclarator =>
       cleanType(ASTTypeUtil.getNodeType(node))
     case _ =>
       import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.getNodeSignature


Test patch:
diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/AstCreationPassTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/AstCreationPassTests.scala
index 613419350761..8afd4984ee76 100644
--- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/AstCreationPassTests.scala
+++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/AstCreationPassTests.scala
@@ -1536,6 +1536,22 @@ class AstCreationPassTests extends AnyWordSpec with Matchers with CpgAstOnlyFixt
       cpg.call.name("<operator>.new").code("new int\\[n\\]").argument.code("int").size shouldBe 1
     }
 
+    // for: https://github.com/ShiftLeftSecurity/codepropertygraph/issues/1526
+    "be correct for array size" in TestAstOnlyFixture("""
+        |int main() {
+        |  char buf[256];
+        |  printf("%s", buf);
+        |}
+        |""".stripMargin) { cpg =>
+      cpg.local.l match {
+        case List(buf: Local) =>
+          buf.typeFullName shouldBe "char[256]"
+          buf.name shouldBe "buf"
+          buf.code shouldBe "buf"
+        case _ => fail()
+      }
+    }
+
     "be correct for array init" in TestAstOnlyFixture("""
         |int x[] = {0, 1, 2, 3};
         |""".stripMargin) { cpg =>
@@ -1607,7 +1623,7 @@ class AstCreationPassTests extends AnyWordSpec with Matchers with CpgAstOnlyFixt
           }
           childrenFoo shouldBe argsFoo
 
-          identBar.typeFullName shouldBe "int[]"
+          identBar.typeFullName shouldBe "int[3]"
           identBar.order shouldBe 1
           barCall.code shouldBe "{0, 1, 2}"
           barCall.order shouldBe 2
@@ -1723,7 +1739,7 @@ class AstCreationPassTests extends AnyWordSpec with Matchers with CpgAstOnlyFixt
       """.stripMargin) { cpg =>
       cpg.assignment.head.astChildren.l match {
         case List(ident: Identifier, call: Call) =>
-          ident.typeFullName shouldBe "int[]"
+          ident.typeFullName shouldBe "int[3]"
           ident.order shouldBe 1
           call.code shouldBe "{ [1] = 5, [2] = 10 }"
           call.order shouldBe 2
@@ -1934,8 +1950,6 @@ class AstCreationPassTests extends AnyWordSpec with Matchers with CpgAstOnlyFixt
        |int c = 0;
        |}
       """.stripMargin) { cpg =>
-      val x = cpg.identifier.l
-      x.foreach(println)
       cpg.identifier.l match {
         case List(a, b, c) =>
           a.lineNumber shouldBe Some(3)
diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/TypeNodePassTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/TypeNodePassTests.scala
index c1480d1dd8bb..48d39f3d5071 100644
--- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/TypeNodePassTests.scala
+++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/TypeNodePassTests.scala
@@ -22,13 +22,13 @@ class TypeNodePassTests extends AnyWordSpec with Matchers with CpgTypeNodeFixtur
        |}""".stripMargin) { cpg =>
       cpg.local.l match {
         case List(test) =>
-          test.typeFullName shouldBe "char[]"
-          test.evalType.l shouldBe List("char[]")
+          test.typeFullName shouldBe "char[1024]"
+          test.evalType.l shouldBe List("char[1024]")
           test.typ.l match {
             case List(t: Type) =>
-              t.name shouldBe "char[]"
-              t.fullName shouldBe "char[]"
-              t.typeDeclFullName shouldBe "char[]"
+              t.name shouldBe "char[1024]"
+              t.fullName shouldBe "char[1024]"
+              t.typeDeclFullName shouldBe "char[1024]"
             case _ => fail()
           }
         case _ => fail()