Skip to content

Commit 8cbf400

Browse files
authored
Merge pull request #1943 from zawata/fix/alloc-dealloc-mismatch
Fix Alloc-Dealloc mismatches
2 parents c999ce2 + 231f472 commit 8cbf400

File tree

6 files changed

+26
-17
lines changed

6 files changed

+26
-17
lines changed

generate/templates/manual/include/configurable_class_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace nodegit {
4040

4141
virtual ~ConfigurableClassWrapper() {
4242
if (raw != nullptr) {
43-
delete raw;
43+
free(raw);
4444
raw = nullptr;
4545
}
4646
}

generate/templates/manual/repository/refresh_references.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ class RefreshedRefModel {
244244
}
245245

246246
~RefreshedRefModel() {
247-
if (fullName != NULL) { delete[] fullName; }
248-
if (message != NULL) { delete[] message; }
247+
if (fullName != NULL) { free(fullName); }
248+
if (message != NULL) { free(message); }
249249
delete[] sha;
250-
if (shorthand != NULL) { delete[] shorthand; }
250+
if (shorthand != NULL) { free(shorthand); }
251251
if (tagOdbBuffer != NULL) { delete[] tagOdbBuffer; }
252252
}
253253

@@ -344,8 +344,8 @@ class UpstreamModel {
344344
}
345345

346346
~UpstreamModel() {
347-
if (downstreamFullName != NULL) { delete[] downstreamFullName; }
348-
if (upstreamFullName != NULL) { delete[] upstreamFullName; }
347+
if (downstreamFullName != NULL) { free(downstreamFullName); }
348+
if (upstreamFullName != NULL) { free(upstreamFullName); }
349349
}
350350

351351
char *downstreamFullName;
@@ -375,7 +375,7 @@ class RefreshReferencesData {
375375
delete upstreamInfo.back();
376376
upstreamInfo.pop_back();
377377
}
378-
if (headRefFullName != NULL) { delete[] headRefFullName; }
378+
if (headRefFullName != NULL) { free(headRefFullName); }
379379
if (cherrypick != NULL) { delete cherrypick; }
380380
if (merge != NULL) { delete merge; }
381381
}
@@ -573,7 +573,7 @@ void GitRepository::RefreshReferencesWorker::Execute()
573573
if (isRemote) {
574574
char *remoteNameOfRef = getRemoteNameOfReference(reference);
575575
bool isFromExistingRemote = gitStrArrayContains(&remoteNames, remoteNameOfRef);
576-
delete[] remoteNameOfRef;
576+
free(remoteNameOfRef);
577577
if (!isFromExistingRemote) {
578578
git_reference_free(reference);
579579
continue;

generate/templates/manual/revwalk/file_history_walk.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class FileHistoryEvent {
3131
if (commit != NULL) {
3232
git_commit_free(commit);
3333
}
34+
35+
if(from != NULL) {
36+
free((void *)from);
37+
}
38+
39+
if(to != NULL) {
40+
free((void *)to);
41+
}
3442
}
3543

3644
v8::Local<v8::Value> toJavascript() {

generate/templates/manual/src/str_array_converter.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ git_strarray *StrArrayConverter::ConstructStrArray(int argc, char** argv) {
6565

6666
void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::Array> val) {
6767
out->count = val->Length();
68-
out->strings = new char *[out->count];
68+
out->strings = (char**) malloc(out->count * sizeof(char*));
6969
for (uint32_t i = 0; i < out->count; ++i) {
7070
Nan::Utf8String utf8String(Nan::Get(val, i).ToLocalChecked().As<v8::String>());
7171
out->strings[i] = strdup(*utf8String);
@@ -75,6 +75,6 @@ void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::Array> val)
7575
void StrArrayConverter::ConvertInto(git_strarray *out, v8::Local<v8::String> val) {
7676
Nan::Utf8String utf8String(val);
7777
out->count = 1;
78-
out->strings = new char *[1];
78+
out->strings = (char**) malloc(out->count * sizeof(char*));
7979
out->strings[0] = strdup(*utf8String);
8080
}

generate/templates/partials/async_function.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,21 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
5656
{% if arg.cppClassName == 'Array' %}
5757
{
5858
v8::Local<v8::Array> tempArray = v8::Local<v8::Array>::Cast(info[{{ arg.jsArg }}]);
59-
baton->{{ arg.name }} = new {{ arg.cType|unPointer }}[tempArray->Length()];
59+
baton->{{ arg.name }} = ({{ arg.cType|unPointer }}*)malloc(sizeof({{ arg.cType|unPointer }}) * tempArray->Length());
6060
for (uint32_t i = 0; i < tempArray->Length(); ++i) {
6161
auto conversionResult = Configurable{{ arg.arrayElementCppClassName }}::fromJavascript(
6262
nodegitContext,
6363
Nan::Get(tempArray, i).ToLocalChecked()
6464
);
6565

6666
if (!conversionResult.result) {
67-
delete[] baton->{{ arg.name }};
67+
// TODO free previously allocated memory
68+
free(baton->{{ arg.name }});
6869
return Nan::ThrowError(Nan::New(conversionResult.error).ToLocalChecked());
6970
}
7071

7172
auto convertedObject = conversionResult.result;
72-
cleanupHandles["{{ arg.name }}"] = convertedObject;
73+
cleanupHandles[std::string("{{ arg.name }}") + std::to_string(i)] = convertedObject;
7374
baton->{{ arg.name }}[i] = *convertedObject->GetValue();
7475
}
7576
}

generate/templates/templates/struct_content.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Configurable{{ cppClassName }}::Configurable{{ cppClassName }}(nodegit::Context
118118
: nodegit::ConfigurableClassWrapper<{{ cppClassName }}Traits>(nodegitContext)
119119
{
120120
{% if ignoreInit == true %}
121-
this->raw = new {{ cType }};
121+
this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}));
122122
{% else %}
123123
{{ cType }}{% if isExtendedStruct %}_extended{% endif %} wrappedValue = {{ cType|upper }}_INIT;
124124
this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}{% if isExtendedStruct %}_extended{% endif %}));
@@ -132,12 +132,12 @@ Configurable{{ cppClassName }}::~Configurable{{ cppClassName }}() {
132132
{% if field.cppClassName == 'GitStrarray' %}
133133
if (this->raw->{{ field.name }}.count) {
134134
for (size_t i = 0; i < this->raw->{{ field.name }}.count; ++i) {
135-
delete this->raw->{{ field.name }}.strings[i];
135+
free(this->raw->{{ field.name }}.strings[i]);
136136
}
137-
delete[] this->raw->{{ field.name }}.strings;
137+
free(this->raw->{{ field.name }}.strings);
138138
}
139139
{% elsif field.cppClassName == 'String' %}
140-
delete this->raw->{{ field.name }};
140+
free((void*)this->raw->{{ field.name }});
141141
{% endif %}
142142
{% endif %}
143143
{% endeach %}

0 commit comments

Comments
 (0)