From 24f5fc626665844ebf48caf2856b7c98b96e9184 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 7 Feb 2022 15:08:48 -0300 Subject: [PATCH 1/2] fix: TypeEncoding might be initialized with random data --- NativeScript/runtime/ClassBuilder.mm | 1 + NativeScript/runtime/InteropTypes.mm | 1 + 2 files changed, 2 insertions(+) diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index ebedae1e..9ba6e172 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -548,6 +548,7 @@ SEL selector = sel_registerName(methodNameStr.c_str()); TypeEncoding* typeEncoding = reinterpret_cast(malloc((argsCount + 1) * sizeof(TypeEncoding))); + memset(typeEncoding, 0, (argsCount + 1) * sizeof(TypeEncoding)); typeEncoding->type = returnType; if (!paramsVal.IsEmpty() && paramsVal->IsArray()) { diff --git a/NativeScript/runtime/InteropTypes.mm b/NativeScript/runtime/InteropTypes.mm index c0b2dc98..dc3c6614 100644 --- a/NativeScript/runtime/InteropTypes.mm +++ b/NativeScript/runtime/InteropTypes.mm @@ -310,6 +310,7 @@ const TypeEncoding* Interop::CreateEncoding(BinaryTypeEncodingType type) { TypeEncoding* typeEncoding = reinterpret_cast(malloc(sizeof(TypeEncoding))); + memset(typeEncoding, 0, sizeof(TypeEncoding)); typeEncoding->type = type; return typeEncoding; From 7f7f8751f7b866cd32ba7ede2915f0e8c1be7bdb Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 7 Feb 2022 15:23:33 -0300 Subject: [PATCH 2/2] chore: use calloc instead o malloc+memset --- NativeScript/runtime/ClassBuilder.mm | 3 +-- NativeScript/runtime/InteropTypes.mm | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index 9ba6e172..4e8de0df 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -547,8 +547,7 @@ std::string methodNameStr = tns::ToString(isolate, methodName); SEL selector = sel_registerName(methodNameStr.c_str()); - TypeEncoding* typeEncoding = reinterpret_cast(malloc((argsCount + 1) * sizeof(TypeEncoding))); - memset(typeEncoding, 0, (argsCount + 1) * sizeof(TypeEncoding)); + TypeEncoding* typeEncoding = reinterpret_cast(calloc((argsCount + 1), sizeof(TypeEncoding))); typeEncoding->type = returnType; if (!paramsVal.IsEmpty() && paramsVal->IsArray()) { diff --git a/NativeScript/runtime/InteropTypes.mm b/NativeScript/runtime/InteropTypes.mm index dc3c6614..c4cd638f 100644 --- a/NativeScript/runtime/InteropTypes.mm +++ b/NativeScript/runtime/InteropTypes.mm @@ -309,8 +309,7 @@ } const TypeEncoding* Interop::CreateEncoding(BinaryTypeEncodingType type) { - TypeEncoding* typeEncoding = reinterpret_cast(malloc(sizeof(TypeEncoding))); - memset(typeEncoding, 0, sizeof(TypeEncoding)); + TypeEncoding* typeEncoding = reinterpret_cast(calloc(1, sizeof(TypeEncoding))); typeEncoding->type = type; return typeEncoding;