Skip to content

Commit 1273fff

Browse files
committed
Audit of memory leaks
1 parent 5de9000 commit 1273fff

39 files changed

+460
-152
lines changed

TODO

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
- audit all ** methods so that all finds go through the repo in js land so that the .repo pointer is set.
2-
- free everything malloc'd, or figure out the right coding conventions for copying.
31
- codegen documentation
4-
- audit return types for things that should be copied
52
- array handling

binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
'src/refdb.cc',
3232
'src/odb_object.cc',
3333
'src/odb.cc',
34-
'src/submodule.cc'
34+
'src/submodule.cc',
35+
'src/functions/copy.cc',
36+
3537
],
3638

3739
'include_dirs': [

include/functions/copy.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <v8.h>
2+
3+
#include "git2.h"
4+
5+
#ifndef COPY_FUNCTIONS
6+
#define COPY_FUNCTIONS
7+
8+
const git_oid *git_oid_dup(const git_oid *arg);
9+
const git_index_entry *git_index_entry_dup(const git_index_entry *arg);
10+
const git_index_time *git_index_time_dup(const git_index_time *arg);
11+
const git_time *git_time_dup(const git_time *arg);
12+
const git_diff_delta *git_diff_delta_dup(const git_diff_delta *arg);
13+
const git_diff_file *git_diff_file_dup(const git_diff_file *arg);
14+
15+
#endif

include/functions/string.h

Lines changed: 0 additions & 8 deletions
This file was deleted.

include/object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class GitObject : public ObjectWrap {
3333

3434
static Handle<Value> Oid(const Arguments& args);
3535
static Handle<Value> Type(const Arguments& args);
36-
static Handle<Value> Owner(const Arguments& args);
3736
static Handle<Value> Peel(const Arguments& args);
3837
static void PeelWork(uv_work_t* req);
3938
static void PeelAfterWork(uv_work_t* req);

lib/diff_list.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ var git = require('../'),
33
ConvenientPatch = require('./convenient_patch').ConvenientPatch,
44
events = require('events');
55

6-
console.log(ConvenientPatch);
7-
86
/**
97
* Refer to vendor/libgit2/include/git2/diff.h for delta type definitions.
108
*

munge.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
var fs = require('fs'),
2-
_ = require('underscore'),
32
path = require('path');
43

4+
var ds = new RegExp('\\*\\*');
5+
56
var idefs = JSON.parse(fs.readFileSync('v0.18.0.json'));
67

8+
var types2free = {};
9+
10+
var oidFns = "git_blob_id git_commit_id git_commit_tree_id git_commit_parent_id git_indexer_stream_hash git_note_oid git_object_id git_odb_object_id git_reflog_entry_id_old git_reflog_entry_id_new git_reference_target git_submodule_index_id git_submodule_head_id git_submodule_wd_id git_tag_id git_tag_target_id git_tree_entry_id git_tree_id".split(' ');
11+
12+
for (var i in idefs) {
13+
var idef = idefs[i];
14+
15+
for (var f in idef.functions) {
16+
var fn = idef.functions[f];
17+
18+
if (oidFns.indexOf(fn.cFunctionName) > -1) {
19+
fn.return.copy = "git_oid_dup";
20+
}
21+
}
22+
}
23+
console.log(JSON.stringify(idefs, null, 2));
24+
25+
return;
26+
727
for (var i in idefs) {
828
var idef = idefs[i];
29+
types2free[idef.cType] = idef.freeFunctionName;
30+
}
31+
32+
for (var t in types2free) {
33+
var re = new RegExp(t);
934

10-
for (var j in idef.functions) {
11-
var fn = idef.functions[j];
12-
if (!fn.isConstructorMethod) continue;
13-
fn.args[0].isReturn = true;
35+
for (var i in idefs) {
36+
var idef = idefs[i];
37+
38+
for (var f in idef.fields) {
39+
var fn = idef.functions[f];
40+
41+
for (var a in fn.args) {
42+
var arg = fn.args[a];
43+
44+
if (t == "git_oid" && arg.isReturn && re.test(arg.cType) && !ds.test(arg.cType)) {
45+
arg.shouldAlloc = true;
46+
}
47+
}
48+
}
1449
}
1550
}
1651
console.log(JSON.stringify(idefs, null, 2));
52+

src/blob.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "git2.h"
99

10+
#include "../include/functions/copy.h"
11+
1012
#include "../include/blob.h"
1113
#include "../include/repo.h"
1214
#include "../include/oid.h"
@@ -77,7 +79,8 @@ Handle<Value> GitBlob::Oid(const Arguments& args) {
7779
);
7880

7981
Handle<Value> to;
80-
to = GitOid::New((void *)result);
82+
result = (const git_oid * )git_oid_dup(result);
83+
to = GitOid::New((void *)result);
8184
return scope.Close(to);
8285
}
8386

@@ -124,6 +127,7 @@ Handle<Value> GitBlob::CreateFromFile(const Arguments& args) {
124127
baton->error_code = GIT_OK;
125128
baton->error = NULL;
126129
baton->request.data = baton;
130+
baton->id = (git_oid *)malloc(sizeof(git_oid ));
127131
baton->repoReference = Persistent<Value>::New(args[0]);
128132
git_repository * from_repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject())->GetValue();
129133
baton->repo = from_repo;
@@ -204,6 +208,7 @@ Handle<Value> GitBlob::CreateFromBuffer(const Arguments& args) {
204208
baton->error_code = GIT_OK;
205209
baton->error = NULL;
206210
baton->request.data = baton;
211+
baton->oid = (git_oid *)malloc(sizeof(git_oid ));
207212
baton->repoReference = Persistent<Value>::New(args[0]);
208213
git_repository * from_repo = ObjectWrap::Unwrap<GitRepo>(args[0]->ToObject())->GetValue();
209214
baton->repo = from_repo;

src/branch.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "git2.h"
99

10+
#include "../include/functions/copy.h"
11+
1012
#include "../include/branch.h"
1113

1214
using namespace v8;

src/commit.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "git2.h"
99

10+
#include "../include/functions/copy.h"
11+
1012
#include "../include/commit.h"
1113
#include "../include/oid.h"
1214
#include "../include/repo.h"
@@ -82,7 +84,8 @@ Handle<Value> GitCommit::Oid(const Arguments& args) {
8284
);
8385

8486
Handle<Value> to;
85-
to = GitOid::New((void *)result);
87+
result = (const git_oid * )git_oid_dup(result);
88+
to = GitOid::New((void *)result);
8689
return scope.Close(to);
8790
}
8891

@@ -147,7 +150,8 @@ Handle<Value> GitCommit::Committer(const Arguments& args) {
147150
);
148151

149152
Handle<Value> to;
150-
to = GitSignature::New((void *)result);
153+
result = (const git_signature * )git_signature_dup(result);
154+
to = GitSignature::New((void *)result);
151155
return scope.Close(to);
152156
}
153157

@@ -160,7 +164,8 @@ Handle<Value> GitCommit::Author(const Arguments& args) {
160164
);
161165

162166
Handle<Value> to;
163-
to = GitSignature::New((void *)result);
167+
result = (const git_signature * )git_signature_dup(result);
168+
to = GitSignature::New((void *)result);
164169
return scope.Close(to);
165170
}
166171

@@ -173,7 +178,8 @@ Handle<Value> GitCommit::TreeId(const Arguments& args) {
173178
);
174179

175180
Handle<Value> to;
176-
to = GitOid::New((void *)result);
181+
result = (const git_oid * )git_oid_dup(result);
182+
to = GitOid::New((void *)result);
177183
return scope.Close(to);
178184
}
179185

@@ -204,7 +210,8 @@ Handle<Value> GitCommit::ParentId(const Arguments& args) {
204210
);
205211

206212
Handle<Value> to;
207-
to = GitOid::New((void *)result);
213+
result = (const git_oid * )git_oid_dup(result);
214+
to = GitOid::New((void *)result);
208215
return scope.Close(to);
209216
}
210217

0 commit comments

Comments
 (0)