From 72bc423f3f485eca2f2d3e11a22ea355604855d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Leuth=C3=A4user?= <1417198+max-leuthaeuser@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:56:22 +0200 Subject: [PATCH] [c2cpg] Restore pointer operator code representation (#4894) --- .../joern/c2cpg/astcreation/AstCreatorHelper.scala | 2 +- .../astcreation/AstForStatementsCreator.scala | 4 ++-- .../c2cpg/passes/ast/AstCreationPassTests.scala | 14 ++++++++++++++ .../io/joern/c2cpg/passes/ast/MethodTests.scala | 2 +- .../joern/c2cpg/passes/types/ClassTypeTests.scala | 6 +++--- 5 files changed, 21 insertions(+), 7 deletions(-) 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 c4d724453dde..1558d64ce73d 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 @@ -330,7 +330,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As } if (pointers.isEmpty) { s"$tpe$arr" } else { - val refs = "*" * (pointers.length - pointers.count(_.isInstanceOf[ICPPASTReferenceOperator])) + val refs = pointers.map(_.getRawSignature).mkString("") s"$tpe$arr$refs".strip() } } diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala index 7f717ee9b8a0..0dcc4b8bc032 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForStatementsCreator.scala @@ -54,11 +54,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t val name = Operators.alloc val tpe = registerType(typeFor(d)) val codeString = code(d) - val allocCallNode = callNode(d, code(d), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(tpe)) + val allocCallNode = callNode(d, codeString, name, name, DispatchTypes.STATIC_DISPATCH, None, Some(tpe)) val allocCallAst = callAst(allocCallNode, d.getArrayModifiers.toIndexedSeq.map(astForNode)) val operatorName = Operators.assignment val assignmentCallNode = - callNode(d, code(d), operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, None, Some(tpe)) + callNode(d, codeString, operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, None, Some(tpe)) val left = astForNode(d.getName) callAst(assignmentCallNode, List(left, allocCallAst)) } diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala index 0edb99cde86f..5f0bbdf7b68e 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala @@ -500,6 +500,20 @@ class AstCreationPassTests extends AstC2CpgSuite { } } + "be correct for decl assignment with references" in { + val cpg = code( + """ + |int addrOfLocalRef(struct x **foo) { + | struct x &bar = **foo; + | *foo = &bar; + |}""".stripMargin, + "foo.cc" + ) + val List(barLocal) = cpg.method.nameExact("addrOfLocalRef").local.l + barLocal.name shouldBe "bar" + barLocal.code shouldBe "struct x& bar" + } + "be correct for decl assignment of multiple locals" in { val cpg = code(""" |void method(int x, int y) { diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/MethodTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/MethodTests.scala index aea6f46b10d3..7825d78d2531 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/MethodTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/MethodTests.scala @@ -94,7 +94,7 @@ class MethodTests extends C2CpgSuite { data.index shouldBe 1 data.name shouldBe "data" data.code shouldBe "int &data" - data.typeFullName shouldBe "int" + data.typeFullName shouldBe "int&" data.isVariadic shouldBe false } } diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/ClassTypeTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/ClassTypeTests.scala index f68af20fc785..c7ac3a2eb5ed 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/ClassTypeTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/ClassTypeTests.scala @@ -172,14 +172,14 @@ class ClassTypeTests extends C2CpgSuite(FileDefaults.CPP_EXT) { | ): Bar::Foo(a, b) {} |}""".stripMargin) val List(constructor) = cpg.typeDecl.nameExact("FooT").method.isConstructor.l - constructor.signature shouldBe "Bar.Foo(std.string,Bar.SomeClass)" + constructor.signature shouldBe "Bar.Foo(std.string&,Bar.SomeClass&)" val List(thisP, p1, p2) = constructor.parameter.l thisP.name shouldBe "this" thisP.typeFullName shouldBe "FooT" thisP.index shouldBe 0 - p1.typ.fullName shouldBe "std.string" + p1.typ.fullName shouldBe "std.string&" p1.index shouldBe 1 - p2.typ.fullName shouldBe "Bar.SomeClass" + p2.typ.fullName shouldBe "Bar.SomeClass&" p2.index shouldBe 2 } }