Skip to content
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

Fix Alloc-Dealloc mismatches #1943

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace nodegit {

virtual ~ConfigurableClassWrapper() {
if (raw != nullptr) {
delete raw;
free(raw);
raw = nullptr;
}
}
Expand Down
14 changes: 7 additions & 7 deletions generate/templates/manual/repository/refresh_references.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions generate/templates/manual/revwalk/file_history_walk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class FileHistoryEvent {
if (commit != NULL) {
git_commit_free(commit);
}

if(from != NULL) {
ianhattendorf marked this conversation as resolved.
Show resolved Hide resolved
free((void *)from);
}

if(to != NULL) {
free((void *)to);
}
}

v8::Local<v8::Value> toJavascript() {
Expand Down
4 changes: 2 additions & 2 deletions generate/templates/manual/src/str_array_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ git_strarray *StrArrayConverter::ConstructStrArray(int argc, char** argv) {

void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::Array> 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<v8::String>());
out->strings[i] = strdup(*utf8String);
Expand All @@ -75,6 +75,6 @@ void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::Array> val)
void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::String> 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);
}
7 changes: 4 additions & 3 deletions generate/templates/partials/async_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
{% if arg.cppClassName == 'Array' %}
{
v8::Local<v8::Array> tempArray = v8::Local<v8::Array>::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,
Nan::Get(tempArray, i).ToLocalChecked()
);

if (!conversionResult.result) {
delete[] baton->{{ arg.name }};
// TODO free previously allocated memory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still need to be handled?

Copy link
Contributor Author

@zawata zawata Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julianmesa-gitkraken this comment came from you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but I will do it in another PR. I need to store all allocated memory in a map, then if I get an error reading javascript arguments we have to release all allocated memory, then throw the error.

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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions generate/templates/templates/struct_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ 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 }}*) malloc(sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %}));
Expand All @@ -132,12 +132,12 @@ 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;
free(this->raw->{{ field.name }}.strings);
}
{% elsif field.cppClassName == 'String' %}
delete this->raw->{{ field.name }};
free((void*)this->raw->{{ field.name }});
{% endif %}
{% endif %}
{% endeach %}
Expand Down