Skip to content

Commit 9cc0ba6

Browse files
authored
Merge pull request libgit2#4226 from libgit2/ethomson/memleak
WIP: squash some memleaks
2 parents 13c1bf0 + 1dc89aa commit 9cc0ba6

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

src/branch.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ int git_branch_create_from_annotated(
130130
static int branch_equals(git_repository *repo, const char *path, void *payload)
131131
{
132132
git_reference *branch = (git_reference *) payload;
133-
git_reference *head;
134-
int equal;
133+
git_reference *head = NULL;
134+
int equal = 0;
135135

136136
if (git_reference__read_head(&head, repo, path) < 0 ||
137-
git_reference_type(head) != GIT_REF_SYMBOLIC)
138-
return 0;
137+
git_reference_type(head) != GIT_REF_SYMBOLIC)
138+
goto done;
139139

140140
equal = !git__strcmp(head->target.symbolic, branch->name);
141+
142+
done:
141143
git_reference_free(head);
142144
return equal;
143145
}

src/odb_loose.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len)
205205
return inflate(s, 0);
206206
}
207207

208+
static void abort_inflate(z_stream *s)
209+
{
210+
inflateEnd(s);
211+
}
212+
208213
static int finish_inflate(z_stream *s)
209214
{
210215
int status = Z_OK;
@@ -367,6 +372,7 @@ static int inflate_disk_obj(git_rawobj *out, git_buf *obj)
367372
(used = get_object_header(&hdr, head)) == 0 ||
368373
!git_object_typeisloose(hdr.type))
369374
{
375+
abort_inflate(&zs);
370376
giterr_set(GITERR_ODB, "failed to inflate disk object");
371377
return -1;
372378
}

src/refs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ int git_reference__read_head(
277277
}
278278

279279
out:
280-
free(name);
281-
git_buf_clear(&reference);
280+
git__free(name);
281+
git_buf_free(&reference);
282282

283283
return error;
284284
}

src/remote.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ static int canonicalize_url(git_buf *out, const char *in)
192192
static int create_internal(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch)
193193
{
194194
git_remote *remote;
195-
git_config *config = NULL;
195+
git_config *config_ro = NULL, *config_rw;
196196
git_buf canonical_url = GIT_BUF_INIT;
197197
git_buf var = GIT_BUF_INIT;
198198
int error = -1;
199199

200200
/* name is optional */
201201
assert(out && repo && url);
202202

203-
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
203+
if ((error = git_repository_config_snapshot(&config_ro, repo)) < 0)
204204
return error;
205205

206206
remote = git__calloc(1, sizeof(git_remote));
@@ -212,7 +212,8 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
212212
(error = canonicalize_url(&canonical_url, url)) < 0)
213213
goto on_error;
214214

215-
remote->url = apply_insteadof(repo->_config, canonical_url.ptr, GIT_DIRECTION_FETCH);
215+
remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
216+
GITERR_CHECK_ALLOC(remote->url);
216217

217218
if (name != NULL) {
218219
remote->name = git__strdup(name);
@@ -221,7 +222,8 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
221222
if ((error = git_buf_printf(&var, CONFIG_URL_FMT, name)) < 0)
222223
goto on_error;
223224

224-
if ((error = git_config_set_string(config, var.ptr, canonical_url.ptr)) < 0)
225+
if ((error = git_repository_config__weakptr(&config_rw, repo)) < 0 ||
226+
(error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0)
225227
goto on_error;
226228
}
227229

@@ -233,10 +235,7 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
233235
if (name && (error = write_add_refspec(repo, name, fetch, true)) < 0)
234236
goto on_error;
235237

236-
if ((error = git_repository_config_snapshot(&config, repo)) < 0)
237-
goto on_error;
238-
239-
if ((error = lookup_remote_prune_config(remote, config, name)) < 0)
238+
if ((error = lookup_remote_prune_config(remote, config_ro, name)) < 0)
240239
goto on_error;
241240

242241
/* Move the data over to where the matching functions can find them */
@@ -260,6 +259,7 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
260259
if (error)
261260
git_remote_free(remote);
262261

262+
git_config_free(config_ro);
263263
git_buf_free(&canonical_url);
264264
git_buf_free(&var);
265265
return error;

src/repository.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,8 @@ int git_repository_head_for_worktree(git_reference **out, git_repository *repo,
21322132
out:
21332133
if (error)
21342134
git_reference_free(head);
2135-
git_buf_clear(&path);
2135+
2136+
git_buf_free(&path);
21362137

21372138
return error;
21382139
}

src/signature.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
228228
const char *time_start = email_end + 2;
229229
const char *time_end;
230230

231-
if (git__strtol64(&sig->when.time, time_start, &time_end, 10) < 0)
231+
if (git__strtol64(&sig->when.time, time_start, &time_end, 10) < 0) {
232+
git__free(sig->name);
233+
git__free(sig->email);
232234
return signature_error("invalid Unix timestamp");
235+
}
233236

234237
/* do we have a timezone? */
235238
if (time_end + 1 < buffer_end) {

tests/object/lookup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
116116
cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
117117
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
118118

119+
git_object_free(object);
119120
git_buf_free(&oldpath);
120121
git_buf_free(&newpath);
121122
}

0 commit comments

Comments
 (0)