Skip to content

Commit

Permalink
Fixing memory leaks when git errors occur
Browse files Browse the repository at this point in the history
  • Loading branch information
nkallen committed Sep 3, 2013
1 parent 79f1115 commit 7f9f3c4
Show file tree
Hide file tree
Showing 15 changed files with 587 additions and 459 deletions.
117 changes: 64 additions & 53 deletions src/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ void GitIndex::OpenAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -200,21 +202,22 @@ void GitIndex::ReadAfterWork(uv_work_t *req) {

TryCatch try_catch;
if (baton->error_code == GIT_OK) {

Handle<Value> result = Local<Value>::New(Undefined());
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -263,21 +266,22 @@ void GitIndex::WriteAfterWork(uv_work_t *req) {

TryCatch try_catch;
if (baton->error_code == GIT_OK) {

Handle<Value> result = Local<Value>::New(Undefined());
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -335,21 +339,22 @@ void GitIndex::ReadTreeAfterWork(uv_work_t *req) {

TryCatch try_catch;
if (baton->error_code == GIT_OK) {

Handle<Value> result = Local<Value>::New(Undefined());
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -414,14 +419,17 @@ void GitIndex::WriteTreeAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
free(baton->out);
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -611,21 +619,22 @@ void GitIndex::AddBypathAfterWork(uv_work_t *req) {

TryCatch try_catch;
if (baton->error_code == GIT_OK) {

Handle<Value> result = Local<Value>::New(Undefined());
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -833,14 +842,16 @@ void GitIndex::IndexToWorkdirAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down
16 changes: 9 additions & 7 deletions src/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,16 @@ void GitObject::PeelAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down
35 changes: 21 additions & 14 deletions src/odb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,16 @@ void GitOdb::ReadAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -473,14 +475,17 @@ void GitOdb::WriteAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
free(baton->out);
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -527,6 +532,7 @@ Handle<Value> GitOdb::Hash(const Arguments& args) {
, from_type
);
if (result != GIT_OK) {
free(out);
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
Expand Down Expand Up @@ -571,6 +577,7 @@ Handle<Value> GitOdb::Hashfile(const Arguments& args) {
);
free((void *)from_path);
if (result != GIT_OK) {
free(out);
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
Expand Down
1 change: 1 addition & 0 deletions src/oid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Handle<Value> GitOid::FromString(const Arguments& args) {
);
free((void *)from_str);
if (result != GIT_OK) {
free(out);
if (giterr_last()) {
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
} else {
Expand Down
66 changes: 37 additions & 29 deletions src/reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,17 @@ void GitReference::OidForNameAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
free(baton->out);
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -290,14 +293,16 @@ void GitReference::ResolveAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -453,14 +458,16 @@ void GitReference::RenameAfterWork(uv_work_t *req) {
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down Expand Up @@ -512,21 +519,22 @@ void GitReference::DeleteAfterWork(uv_work_t *req) {

TryCatch try_catch;
if (baton->error_code == GIT_OK) {

Handle<Value> result = Local<Value>::New(Undefined());
Handle<Value> argv[2] = {
Local<Value>::New(Null()),
result
};
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
} else if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
if (baton->error) {
Handle<Value> argv[1] = {
Exception::Error(String::New(baton->error->message))
};
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
}
}

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
Expand Down
Loading

0 comments on commit 7f9f3c4

Please sign in to comment.