From e14fafb2d11540980bf7086e14625431a3fd8829 Mon Sep 17 00:00:00 2001 From: John Alden Date: Sat, 15 Oct 2022 01:55:32 -0700 Subject: [PATCH 1/3] Fix Alloc-Dealloc Mismatches --- .../manual/repository/refresh_references.cc | 14 +++++++------- .../templates/manual/revwalk/file_history_walk.cc | 8 ++++++++ generate/templates/templates/struct_content.cc | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/generate/templates/manual/repository/refresh_references.cc b/generate/templates/manual/repository/refresh_references.cc index 5ddaec57f..5194f1c48 100644 --- a/generate/templates/manual/repository/refresh_references.cc +++ b/generate/templates/manual/repository/refresh_references.cc @@ -244,10 +244,10 @@ class RefreshedRefModel { } ~RefreshedRefModel() { - if (fullName != NULL) { delete[] fullName; } - if (message != NULL) { delete[] message; } + if (fullName != NULL) { free(fullName); } + if (message != NULL) { free(message); } delete[] sha; - if (shorthand != NULL) { delete[] shorthand; } + if (shorthand != NULL) { free(shorthand); } if (tagOdbBuffer != NULL) { delete[] tagOdbBuffer; } } @@ -344,8 +344,8 @@ class UpstreamModel { } ~UpstreamModel() { - if (downstreamFullName != NULL) { delete[] downstreamFullName; } - if (upstreamFullName != NULL) { delete[] upstreamFullName; } + if (downstreamFullName != NULL) { free(downstreamFullName); } + if (upstreamFullName != NULL) { free(upstreamFullName); } } char *downstreamFullName; @@ -375,7 +375,7 @@ class RefreshReferencesData { delete upstreamInfo.back(); upstreamInfo.pop_back(); } - if (headRefFullName != NULL) { delete[] headRefFullName; } + if (headRefFullName != NULL) { free(headRefFullName); } if (cherrypick != NULL) { delete cherrypick; } if (merge != NULL) { delete merge; } } @@ -573,7 +573,7 @@ void GitRepository::RefreshReferencesWorker::Execute() if (isRemote) { char *remoteNameOfRef = getRemoteNameOfReference(reference); bool isFromExistingRemote = gitStrArrayContains(&remoteNames, remoteNameOfRef); - delete[] remoteNameOfRef; + free(remoteNameOfRef); if (!isFromExistingRemote) { git_reference_free(reference); continue; diff --git a/generate/templates/manual/revwalk/file_history_walk.cc b/generate/templates/manual/revwalk/file_history_walk.cc index 714d6f5db..569bb022d 100644 --- a/generate/templates/manual/revwalk/file_history_walk.cc +++ b/generate/templates/manual/revwalk/file_history_walk.cc @@ -31,6 +31,14 @@ class FileHistoryEvent { if (commit != NULL) { git_commit_free(commit); } + + if(from != NULL) { + free((void *)from); + } + + if(to != NULL) { + free((void *)to); + } } v8::Local toJavascript() { diff --git a/generate/templates/templates/struct_content.cc b/generate/templates/templates/struct_content.cc index e2f01ca58..d79396f76 100644 --- a/generate/templates/templates/struct_content.cc +++ b/generate/templates/templates/struct_content.cc @@ -121,7 +121,7 @@ Configurable{{ cppClassName }}::Configurable{{ cppClassName }}(nodegit::Context this->raw = new {{ cType }}; {% else %} {{ cType }}{% if isExtendedStruct %}_extended{% endif %} wrappedValue = {{ cType|upper }}_INIT; - this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %})); + this->raw = ({{ cType }}*) new {{ cType }}{% if isExtendedStruct %}_extended{% endif %}; memcpy(this->raw, &wrappedValue, sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %})); {% endif %} } @@ -132,7 +132,7 @@ Configurable{{ cppClassName }}::~Configurable{{ cppClassName }}() { {% if field.cppClassName == 'GitStrarray' %} if (this->raw->{{ field.name }}.count) { for (size_t i = 0; i < this->raw->{{ field.name }}.count; ++i) { - delete this->raw->{{ field.name }}.strings[i]; + free(this->raw->{{ field.name }}.strings[i]); } delete[] this->raw->{{ field.name }}.strings; } From 402f61a9a73afb67ac94fc8779a777d8a608e3d7 Mon Sep 17 00:00:00 2001 From: John Alden Date: Tue, 18 Oct 2022 11:03:17 -0700 Subject: [PATCH 2/3] add a couple more fixes from julian --- .../templates/manual/include/configurable_class_wrapper.h | 2 +- generate/templates/manual/src/str_array_converter.cc | 4 ++-- generate/templates/templates/struct_content.cc | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/generate/templates/manual/include/configurable_class_wrapper.h b/generate/templates/manual/include/configurable_class_wrapper.h index 283eb7e44..ff83a3176 100644 --- a/generate/templates/manual/include/configurable_class_wrapper.h +++ b/generate/templates/manual/include/configurable_class_wrapper.h @@ -40,7 +40,7 @@ namespace nodegit { virtual ~ConfigurableClassWrapper() { if (raw != nullptr) { - delete raw; + free(raw); raw = nullptr; } } diff --git a/generate/templates/manual/src/str_array_converter.cc b/generate/templates/manual/src/str_array_converter.cc index 732f16cf2..5d04c6562 100644 --- a/generate/templates/manual/src/str_array_converter.cc +++ b/generate/templates/manual/src/str_array_converter.cc @@ -65,7 +65,7 @@ git_strarray *StrArrayConverter::ConstructStrArray(int argc, char** argv) { void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local val) { out->count = val->Length(); - out->strings = new char *[out->count]; + out->strings = (char**) malloc(out->count * sizeof(char*)); for (uint32_t i = 0; i < out->count; ++i) { Nan::Utf8String utf8String(Nan::Get(val, i).ToLocalChecked().As()); out->strings[i] = strdup(*utf8String); @@ -75,6 +75,6 @@ void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local val) void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local val) { Nan::Utf8String utf8String(val); out->count = 1; - out->strings = new char *[1]; + out->strings = (char**) malloc(out->count * sizeof(char*)); out->strings[0] = strdup(*utf8String); } diff --git a/generate/templates/templates/struct_content.cc b/generate/templates/templates/struct_content.cc index d79396f76..d7355d2a2 100644 --- a/generate/templates/templates/struct_content.cc +++ b/generate/templates/templates/struct_content.cc @@ -118,10 +118,10 @@ Configurable{{ cppClassName }}::Configurable{{ cppClassName }}(nodegit::Context : nodegit::ConfigurableClassWrapper<{{ cppClassName }}Traits>(nodegitContext) { {% if ignoreInit == true %} - this->raw = new {{ cType }}; + this->raw = ({{ cType }}*) malloc(sizeof({{ cType }})); {% else %} {{ cType }}{% if isExtendedStruct %}_extended{% endif %} wrappedValue = {{ cType|upper }}_INIT; - this->raw = ({{ cType }}*) new {{ cType }}{% if isExtendedStruct %}_extended{% endif %}; + this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %})); memcpy(this->raw, &wrappedValue, sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %})); {% endif %} } @@ -134,10 +134,10 @@ Configurable{{ cppClassName }}::~Configurable{{ cppClassName }}() { for (size_t i = 0; i < this->raw->{{ field.name }}.count; ++i) { free(this->raw->{{ field.name }}.strings[i]); } - delete[] this->raw->{{ field.name }}.strings; + free(this->raw->{{ field.name }}.strings); } {% elsif field.cppClassName == 'String' %} - delete this->raw->{{ field.name }}; + free((void*)this->raw->{{ field.name }}); {% endif %} {% endif %} {% endeach %} From af08eb960ae7b229c2f6d1522852c3b66b816966 Mon Sep 17 00:00:00 2001 From: John Alden Date: Wed, 19 Oct 2022 10:08:49 -0700 Subject: [PATCH 3/3] add yet more fixes from Julian --- generate/templates/partials/async_function.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/generate/templates/partials/async_function.cc b/generate/templates/partials/async_function.cc index 6041c16f3..a40044b7a 100644 --- a/generate/templates/partials/async_function.cc +++ b/generate/templates/partials/async_function.cc @@ -56,7 +56,7 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) { {% if arg.cppClassName == 'Array' %} { v8::Local tempArray = v8::Local::Cast(info[{{ arg.jsArg }}]); - baton->{{ arg.name }} = new {{ arg.cType|unPointer }}[tempArray->Length()]; + baton->{{ arg.name }} = ({{ arg.cType|unPointer }}*)malloc(sizeof({{ arg.cType|unPointer }}) * tempArray->Length()); for (uint32_t i = 0; i < tempArray->Length(); ++i) { auto conversionResult = Configurable{{ arg.arrayElementCppClassName }}::fromJavascript( nodegitContext, @@ -64,12 +64,13 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) { ); if (!conversionResult.result) { - delete[] baton->{{ arg.name }}; + // TODO free previously allocated memory + free(baton->{{ arg.name }}); return Nan::ThrowError(Nan::New(conversionResult.error).ToLocalChecked()); } auto convertedObject = conversionResult.result; - cleanupHandles["{{ arg.name }}"] = convertedObject; + cleanupHandles[std::string("{{ arg.name }}") + std::to_string(i)] = convertedObject; baton->{{ arg.name }}[i] = *convertedObject->GetValue(); } }