Skip to content

Commit 7f9f3c4

Browse files
committed
Fixing memory leaks when git errors occur
1 parent 79f1115 commit 7f9f3c4

15 files changed

+587
-459
lines changed

src/index.cc

+64-53
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ void GitIndex::OpenAfterWork(uv_work_t *req) {
143143
result
144144
};
145145
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
146-
} else if (baton->error) {
147-
Handle<Value> argv[1] = {
148-
Exception::Error(String::New(baton->error->message))
149-
};
150-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
151146
} else {
152-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
153-
}
147+
if (baton->error) {
148+
Handle<Value> argv[1] = {
149+
Exception::Error(String::New(baton->error->message))
150+
};
151+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
152+
} else {
153+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
154+
}
155+
}
154156

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

201203
TryCatch try_catch;
202204
if (baton->error_code == GIT_OK) {
203-
204205
Handle<Value> result = Local<Value>::New(Undefined());
205206
Handle<Value> argv[2] = {
206207
Local<Value>::New(Null()),
207208
result
208209
};
209210
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
210-
} else if (baton->error) {
211-
Handle<Value> argv[1] = {
212-
Exception::Error(String::New(baton->error->message))
213-
};
214-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
215211
} else {
216-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
217-
}
212+
if (baton->error) {
213+
Handle<Value> argv[1] = {
214+
Exception::Error(String::New(baton->error->message))
215+
};
216+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
217+
} else {
218+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
219+
}
220+
}
218221

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

264267
TryCatch try_catch;
265268
if (baton->error_code == GIT_OK) {
266-
267269
Handle<Value> result = Local<Value>::New(Undefined());
268270
Handle<Value> argv[2] = {
269271
Local<Value>::New(Null()),
270272
result
271273
};
272274
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
273-
} else if (baton->error) {
274-
Handle<Value> argv[1] = {
275-
Exception::Error(String::New(baton->error->message))
276-
};
277-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
278275
} else {
279-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
280-
}
276+
if (baton->error) {
277+
Handle<Value> argv[1] = {
278+
Exception::Error(String::New(baton->error->message))
279+
};
280+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
281+
} else {
282+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
283+
}
284+
}
281285

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

336340
TryCatch try_catch;
337341
if (baton->error_code == GIT_OK) {
338-
339342
Handle<Value> result = Local<Value>::New(Undefined());
340343
Handle<Value> argv[2] = {
341344
Local<Value>::New(Null()),
342345
result
343346
};
344347
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
345-
} else if (baton->error) {
346-
Handle<Value> argv[1] = {
347-
Exception::Error(String::New(baton->error->message))
348-
};
349-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
350348
} else {
351-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
352-
}
349+
if (baton->error) {
350+
Handle<Value> argv[1] = {
351+
Exception::Error(String::New(baton->error->message))
352+
};
353+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
354+
} else {
355+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
356+
}
357+
}
353358

354359
if (try_catch.HasCaught()) {
355360
node::FatalException(try_catch);
@@ -414,14 +419,17 @@ void GitIndex::WriteTreeAfterWork(uv_work_t *req) {
414419
result
415420
};
416421
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
417-
} else if (baton->error) {
418-
Handle<Value> argv[1] = {
419-
Exception::Error(String::New(baton->error->message))
420-
};
421-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
422422
} else {
423-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
424-
}
423+
if (baton->error) {
424+
Handle<Value> argv[1] = {
425+
Exception::Error(String::New(baton->error->message))
426+
};
427+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
428+
} else {
429+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
430+
}
431+
free(baton->out);
432+
}
425433

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

612620
TryCatch try_catch;
613621
if (baton->error_code == GIT_OK) {
614-
615622
Handle<Value> result = Local<Value>::New(Undefined());
616623
Handle<Value> argv[2] = {
617624
Local<Value>::New(Null()),
618625
result
619626
};
620627
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
621-
} else if (baton->error) {
622-
Handle<Value> argv[1] = {
623-
Exception::Error(String::New(baton->error->message))
624-
};
625-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
626628
} else {
627-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
628-
}
629+
if (baton->error) {
630+
Handle<Value> argv[1] = {
631+
Exception::Error(String::New(baton->error->message))
632+
};
633+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
634+
} else {
635+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
636+
}
637+
}
629638

630639
if (try_catch.HasCaught()) {
631640
node::FatalException(try_catch);
@@ -833,14 +842,16 @@ void GitIndex::IndexToWorkdirAfterWork(uv_work_t *req) {
833842
result
834843
};
835844
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
836-
} else if (baton->error) {
837-
Handle<Value> argv[1] = {
838-
Exception::Error(String::New(baton->error->message))
839-
};
840-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
841845
} else {
842-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
843-
}
846+
if (baton->error) {
847+
Handle<Value> argv[1] = {
848+
Exception::Error(String::New(baton->error->message))
849+
};
850+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
851+
} else {
852+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
853+
}
854+
}
844855

845856
if (try_catch.HasCaught()) {
846857
node::FatalException(try_catch);

src/object.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ void GitObject::PeelAfterWork(uv_work_t *req) {
166166
result
167167
};
168168
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
169-
} else if (baton->error) {
170-
Handle<Value> argv[1] = {
171-
Exception::Error(String::New(baton->error->message))
172-
};
173-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
174169
} else {
175-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
176-
}
170+
if (baton->error) {
171+
Handle<Value> argv[1] = {
172+
Exception::Error(String::New(baton->error->message))
173+
};
174+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
175+
} else {
176+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
177+
}
178+
}
177179

178180
if (try_catch.HasCaught()) {
179181
node::FatalException(try_catch);

src/odb.cc

+21-14
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,16 @@ void GitOdb::ReadAfterWork(uv_work_t *req) {
230230
result
231231
};
232232
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
233-
} else if (baton->error) {
234-
Handle<Value> argv[1] = {
235-
Exception::Error(String::New(baton->error->message))
236-
};
237-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
238233
} else {
239-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
240-
}
234+
if (baton->error) {
235+
Handle<Value> argv[1] = {
236+
Exception::Error(String::New(baton->error->message))
237+
};
238+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
239+
} else {
240+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
241+
}
242+
}
241243

242244
if (try_catch.HasCaught()) {
243245
node::FatalException(try_catch);
@@ -473,14 +475,17 @@ void GitOdb::WriteAfterWork(uv_work_t *req) {
473475
result
474476
};
475477
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
476-
} else if (baton->error) {
477-
Handle<Value> argv[1] = {
478-
Exception::Error(String::New(baton->error->message))
479-
};
480-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
481478
} else {
482-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
483-
}
479+
if (baton->error) {
480+
Handle<Value> argv[1] = {
481+
Exception::Error(String::New(baton->error->message))
482+
};
483+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
484+
} else {
485+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
486+
}
487+
free(baton->out);
488+
}
484489

485490
if (try_catch.HasCaught()) {
486491
node::FatalException(try_catch);
@@ -527,6 +532,7 @@ Handle<Value> GitOdb::Hash(const Arguments& args) {
527532
, from_type
528533
);
529534
if (result != GIT_OK) {
535+
free(out);
530536
if (giterr_last()) {
531537
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
532538
} else {
@@ -571,6 +577,7 @@ Handle<Value> GitOdb::Hashfile(const Arguments& args) {
571577
);
572578
free((void *)from_path);
573579
if (result != GIT_OK) {
580+
free(out);
574581
if (giterr_last()) {
575582
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
576583
} else {

src/oid.cc

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Handle<Value> GitOid::FromString(const Arguments& args) {
8383
);
8484
free((void *)from_str);
8585
if (result != GIT_OK) {
86+
free(out);
8687
if (giterr_last()) {
8788
return ThrowException(Exception::Error(String::New(giterr_last()->message)));
8889
} else {

src/reference.cc

+37-29
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,17 @@ void GitReference::OidForNameAfterWork(uv_work_t *req) {
147147
result
148148
};
149149
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
150-
} else if (baton->error) {
151-
Handle<Value> argv[1] = {
152-
Exception::Error(String::New(baton->error->message))
153-
};
154-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
155150
} else {
156-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
157-
}
151+
if (baton->error) {
152+
Handle<Value> argv[1] = {
153+
Exception::Error(String::New(baton->error->message))
154+
};
155+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
156+
} else {
157+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
158+
}
159+
free(baton->out);
160+
}
158161

159162
if (try_catch.HasCaught()) {
160163
node::FatalException(try_catch);
@@ -290,14 +293,16 @@ void GitReference::ResolveAfterWork(uv_work_t *req) {
290293
result
291294
};
292295
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
293-
} else if (baton->error) {
294-
Handle<Value> argv[1] = {
295-
Exception::Error(String::New(baton->error->message))
296-
};
297-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
298296
} else {
299-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
300-
}
297+
if (baton->error) {
298+
Handle<Value> argv[1] = {
299+
Exception::Error(String::New(baton->error->message))
300+
};
301+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
302+
} else {
303+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
304+
}
305+
}
301306

302307
if (try_catch.HasCaught()) {
303308
node::FatalException(try_catch);
@@ -453,14 +458,16 @@ void GitReference::RenameAfterWork(uv_work_t *req) {
453458
result
454459
};
455460
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
456-
} else if (baton->error) {
457-
Handle<Value> argv[1] = {
458-
Exception::Error(String::New(baton->error->message))
459-
};
460-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
461461
} else {
462-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
463-
}
462+
if (baton->error) {
463+
Handle<Value> argv[1] = {
464+
Exception::Error(String::New(baton->error->message))
465+
};
466+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
467+
} else {
468+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
469+
}
470+
}
464471

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

513520
TryCatch try_catch;
514521
if (baton->error_code == GIT_OK) {
515-
516522
Handle<Value> result = Local<Value>::New(Undefined());
517523
Handle<Value> argv[2] = {
518524
Local<Value>::New(Null()),
519525
result
520526
};
521527
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
522-
} else if (baton->error) {
523-
Handle<Value> argv[1] = {
524-
Exception::Error(String::New(baton->error->message))
525-
};
526-
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
527528
} else {
528-
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
529-
}
529+
if (baton->error) {
530+
Handle<Value> argv[1] = {
531+
Exception::Error(String::New(baton->error->message))
532+
};
533+
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
534+
} else {
535+
baton->callback->Call(Context::GetCurrent()->Global(), 0, NULL);
536+
}
537+
}
530538

531539
if (try_catch.HasCaught()) {
532540
node::FatalException(try_catch);

0 commit comments

Comments
 (0)