From a7fb18171545745df61c4e64fb62e291aaaaa526 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Fri, 29 Sep 2023 12:34:39 +0200 Subject: [PATCH] Adding unsafe types to Go --- .../aisec/cpg/frontends/golang/GoLanguage.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguage.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguage.kt index 908d450b251..6cb5deb97e7 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguage.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguage.kt @@ -111,7 +111,11 @@ class GoLanguage : // TODO: Actually, this should be a type alias to uint8 "byte" to IntegerType("uint8", 8, this, NumericType.Modifier.UNSIGNED), // https://pkg.go.dev/builtin#string - "string" to StringType("string", this) + "string" to StringType("string", this), + // https://go.dev/ref/spec#Package_unsafe + "unsafe.ArbitraryType" to ObjectType("unsafe.ArbitraryType", listOf(), false, this), + // https://go.dev/ref/spec#Package_unsafe + "unsafe.IntegerType" to ObjectType("unsafe.IntegerType", listOf(), false, this) ) override fun isDerivedFrom( @@ -120,7 +124,20 @@ class GoLanguage : hint: HasType?, superHint: HasType? ): Boolean { - if (type == superType || superType == primitiveType("any")) { + if ( + type == superType || + // "any" accepts any type + superType == primitiveType("any") || + // the unsafe.ArbitraryType is a fake type in the unsafe package, that also accepts + // any type + superType == primitiveType("unsafe.ArbitraryType") + ) { + return true + } + + // the unsafe.IntegerType is a fake type in the unsafe package, that accepts any integer + // type + if (type is IntegerType && superType == primitiveType("unsafe.IntegerType")) { return true }