Skip to content

Commit

Permalink
[c2cpg] Fixed array decl type/code for long number literals (#4170)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Feb 14, 2024
1 parent 33ddb87 commit 0cc41d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,19 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
val tpe = getNodeSignature(a).replace("[]", "").strip()
val arr = ASTTypeUtil.getNodeType(a).replace("? ", "")
s"$tpe$arr"
case a: IASTArrayDeclarator if ASTTypeUtil.getNodeType(a).contains("} ") =>
val tpe = getNodeSignature(a).replace("[]", "").strip()
val nodeType = ASTTypeUtil.getNodeType(node)
val arr = nodeType.substring(nodeType.indexOf("["), nodeType.indexOf("]") + 1)
case a: IASTArrayDeclarator
if ASTTypeUtil.getNodeType(a).contains("} ") || ASTTypeUtil.getNodeType(a).contains(" [") =>
val tpe = getNodeSignature(a).replace("[]", "").strip()
val arr = a.getArrayModifiers.map {
case m if m.getConstantExpression != null => s"[${nodeSignature(m.getConstantExpression)}]"
case _ if a.getInitializer != null =>
a.getInitializer match {
case l: IASTInitializerList => s"[${l.getSize}]"
case _ => "[]"
}
case _ => "[]"
}.mkString
s"$tpe$arr"
case a: IASTArrayDeclarator if ASTTypeUtil.getNodeType(a).contains(" [") =>
cleanType(ASTTypeUtil.getNodeType(node))
case s: CPPASTIdExpression =>
s.getEvaluation match {
case evaluation: EvalMemberAccess =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,7 @@ class AstCreationPassTests extends AbstractPassTest {
|char *a(char *y) {
| char *x;
|}
""".stripMargin) { cpg =>
|""".stripMargin) { cpg =>
cpg.member.name("z").typeFullName.head shouldBe "char*"
cpg.parameter.name("y").typeFullName.head shouldBe "char*"
cpg.local.name("x").typeFullName.head shouldBe "char*"
Expand All @@ -2007,10 +2007,23 @@ class AstCreationPassTests extends AbstractPassTest {
|void a(char y[1]) {
| char x[1];
|}
""".stripMargin) { cpg =>
|""".stripMargin) { cpg =>
cpg.member.name("z").typeFullName.head shouldBe "char[1]"
cpg.parameter.name("y").typeFullName.head shouldBe "char[1]"
cpg.local.name("x").typeFullName.head shouldBe "char[1]"
}

"be consistent with long number types" in AstFixture("""
|#define BUFSIZE 0x111111111111111
|void copy(char *string) {
| char buf[BUFSIZE];
| stpncpy(buf, string, BUFSIZE);
|}
|""".stripMargin) { cpg =>
val List(bufLocal) = cpg.local.nameExact("buf").l
bufLocal.typeFullName shouldBe "char[0x111111111111111]"
bufLocal.code shouldBe "char[0x111111111111111] buf"
cpg.literal.code.l shouldBe List("0x111111111111111")
}
}
}

0 comments on commit 0cc41d5

Please sign in to comment.