Skip to content

fix: Memory leak when marshalling C strings parameters #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions NativeScript/runtime/Interop.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ExtVector.h"
#include "SymbolIterator.h"
#include "UnmanagedType.h"
#include "OneByteStringResource.h"

using namespace v8;

Expand Down Expand Up @@ -165,9 +166,19 @@
Interop::SetValue(dest, selector);
} else if (typeEncoding->type == BinaryTypeEncodingType::CStringEncoding) {
if (arg->IsString()) {
v8::String::Utf8Value utf8Value(isolate, arg);
const char* strCopy = strdup(*utf8Value);
Interop::SetValue(dest, strCopy);
const char* value = nullptr;
Local<v8::String> strArg = arg.As<v8::String>();
if (strArg->IsExternalOneByte()) {
const v8::String::ExternalOneByteStringResource* resource = strArg->GetExternalOneByteStringResource();
value = resource->data();
} else {
v8::String::Utf8Value utf8Value(isolate, arg);
value = strdup(*utf8Value);
OneByteStringResource* resource = new OneByteStringResource(value, strArg->Length());
bool success = v8::String::NewExternalOneByte(isolate, resource).ToLocal(&arg);
tns::Assert(success, isolate);
}
Interop::SetValue(dest, value);
} else {
BaseDataWrapper* wrapper = tns::GetValue(isolate, arg);
if (wrapper == nullptr) {
Expand Down
23 changes: 23 additions & 0 deletions NativeScript/runtime/OneByteStringResource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "OneByteStringResource.h"

using namespace v8;

namespace tns {

OneByteStringResource::OneByteStringResource(const char* data, size_t length):
data_(data), length_(length) {
}

OneByteStringResource::~OneByteStringResource() {
delete this->data_;
}

const char* OneByteStringResource::data() const {
return this->data_;
}

size_t OneByteStringResource::length() const {
return this->length_;
}

}
21 changes: 21 additions & 0 deletions NativeScript/runtime/OneByteStringResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef OneByteStringResource_h
#define OneByteStringResource_h

#include "Common.h"

namespace tns {

class OneByteStringResource : public v8::String::ExternalOneByteStringResource {
public:
OneByteStringResource(const char* data, size_t length);
~OneByteStringResource() override;
const char* data() const override;
size_t length() const override;
private:
const char* data_;
size_t length_;
};

}

#endif /* OneByteStringResource_h */
8 changes: 8 additions & 0 deletions v8ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
C205257F2577D6F900C12A5C /* NativeScript.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; };
C20525802577D6F900C12A5C /* NativeScript.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C2DDEB32229EAB3B00345BFE /* NativeScript.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C20525A82577D86600C12A5C /* TNSWidgets.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C20525A72577D86600C12A5C /* TNSWidgets.xcframework */; };
C20AB5E626E1015300E2B41D /* OneByteStringResource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C20AB5E426E1015200E2B41D /* OneByteStringResource.cpp */; };
C20AB5E726E1015300E2B41D /* OneByteStringResource.h in Headers */ = {isa = PBXBuildFile; fileRef = C20AB5E526E1015200E2B41D /* OneByteStringResource.h */; };
C2229973235449B400C1DFD6 /* InspectorServer.mm in Sources */ = {isa = PBXBuildFile; fileRef = C2229971235449B300C1DFD6 /* InspectorServer.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
C2229974235449B400C1DFD6 /* InspectorServer.h in Headers */ = {isa = PBXBuildFile; fileRef = C2229972235449B400C1DFD6 /* InspectorServer.h */; };
C2229977235492EC00C1DFD6 /* v8-ns-debugger-agent-impl.h in Headers */ = {isa = PBXBuildFile; fileRef = C2229975235492EC00C1DFD6 /* v8-ns-debugger-agent-impl.h */; };
Expand Down Expand Up @@ -541,6 +543,8 @@
2B7EA6AE2353477000E5184E /* NativeScriptException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NativeScriptException.h; sourceTree = "<group>"; };
C2003F9E23FA78CD0043B815 /* TNSDerivedClass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TNSDerivedClass.h; sourceTree = "<group>"; };
C20525A72577D86600C12A5C /* TNSWidgets.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = TNSWidgets.xcframework; sourceTree = "<group>"; };
C20AB5E426E1015200E2B41D /* OneByteStringResource.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OneByteStringResource.cpp; sourceTree = "<group>"; };
C20AB5E526E1015200E2B41D /* OneByteStringResource.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneByteStringResource.h; sourceTree = "<group>"; };
C2229971235449B300C1DFD6 /* InspectorServer.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = InspectorServer.mm; sourceTree = "<group>"; };
C2229972235449B400C1DFD6 /* InspectorServer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InspectorServer.h; sourceTree = "<group>"; };
C2229975235492EC00C1DFD6 /* v8-ns-debugger-agent-impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "v8-ns-debugger-agent-impl.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1893,6 +1897,8 @@
C266567922AA630F00EE15CC /* NSDataAdapter.mm */,
C2DDEB83229EAC8300345BFE /* ObjectManager.h */,
C2DDEB86229EAC8300345BFE /* ObjectManager.mm */,
C20AB5E526E1015200E2B41D /* OneByteStringResource.h */,
C20AB5E426E1015200E2B41D /* OneByteStringResource.cpp */,
C266569222AFFF7E00EE15CC /* Pointer.h */,
C266569122AFFF7E00EE15CC /* Pointer.cpp */,
C2D7E9D323F42C1100DB289C /* PromiseProxy.h */,
Expand Down Expand Up @@ -1977,6 +1983,7 @@
C22536B5241A318900192740 /* ffitarget.h in Headers */,
C2D7E9CF23F4294800DB289C /* export-template.h in Headers */,
C2DDEB97229EAC8300345BFE /* Metadata.h in Headers */,
C20AB5E726E1015300E2B41D /* OneByteStringResource.h in Headers */,
C2DDEBAC229EAC8300345BFE /* ObjectManager.h in Headers */,
C247C32E22F828E3001D2CA2 /* base64.h in Headers */,
C247C35322F828E3001D2CA2 /* v8-runtime-agent-impl.h in Headers */,
Expand Down Expand Up @@ -2629,6 +2636,7 @@
buildActionMask = 2147483647;
files = (
C247C36822F828E3001D2CA2 /* Console.cpp in Sources */,
C20AB5E626E1015300E2B41D /* OneByteStringResource.cpp in Sources */,
C247C36422F828E3001D2CA2 /* Log.cpp in Sources */,
C247C34922F828E3001D2CA2 /* v8-string-conversions.cc in Sources */,
C247C38C22F828E3001D2CA2 /* v8-network-agent-impl.cpp in Sources */,
Expand Down