From 9146f1e57ec4f2b6fa293c78d54f1383464ff5be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Duraffort?= Date: Mon, 15 Jul 2013 15:59:18 +0200 Subject: [PATCH 1/4] repository: clarify assignment and test order --- src/repository.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/repository.c b/src/repository.c index bd7ef5476..99ac56ef9 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1500,12 +1500,12 @@ int git_repository_is_empty(git_repository *repo) if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) return -1; - if (!(error = git_reference_type(head) == GIT_REF_SYMBOLIC)) + if (!((error = git_reference_type(head)) == GIT_REF_SYMBOLIC)) goto cleanup; - if (!(error = strcmp( + if (!(error = (strcmp( git_reference_symbolic_target(head), - GIT_REFS_HEADS_DIR "master") == 0)) + GIT_REFS_HEADS_DIR "master") == 0))) goto cleanup; error = repo_contains_no_reference(repo); From 8d6ef4bf78cc5d3a3cb277ecc4fcf0fdcdbc9f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Duraffort?= Date: Mon, 15 Jul 2013 15:59:35 +0200 Subject: [PATCH 2/4] index: fix potential memory leaks --- src/index.c | 12 +++++++++--- src/indexer.c | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.c b/src/index.c index bd5e192f3..0610eb5b9 100644 --- a/src/index.c +++ b/src/index.c @@ -1409,14 +1409,18 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) if (git__strtol32(&tmp, buffer, &endptr, 8) < 0 || !endptr || endptr == buffer || *endptr || - (unsigned)tmp > UINT_MAX) + (unsigned)tmp > UINT_MAX) { + index_entry_reuc_free(lost); return index_error_invalid("reading reuc entry stage"); + } lost->mode[i] = tmp; len = (endptr + 1) - buffer; - if (size <= len) + if (size <= len) { + index_entry_reuc_free(lost); return index_error_invalid("reading reuc entry stage"); + } size -= len; buffer += len; @@ -1426,8 +1430,10 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) for (i = 0; i < 3; i++) { if (!lost->mode[i]) continue; - if (size < 20) + if (size < 20) { + index_entry_reuc_free(lost); return index_error_invalid("reading reuc entry oid"); + } git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer); size -= 20; diff --git a/src/indexer.c b/src/indexer.c index 1b638cd8a..09f962934 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -325,7 +325,7 @@ static int hash_and_save(git_indexer_stream *idx, git_rawobj *obj, git_off_t ent /* FIXME: Parse the object instead of hashing it */ if (git_odb__hashobj(&oid, obj) < 0) { giterr_set(GITERR_INDEXER, "Failed to hash object"); - return -1; + goto on_error; } pentry = git__calloc(1, sizeof(struct git_pack_entry)); From 050af8bbe08b7cab7bfce044dcb51fb61ff1dc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Duraffort?= Date: Mon, 15 Jul 2013 16:00:00 +0200 Subject: [PATCH 3/4] pack: fix memory leak in error path --- src/pack-objects.c | 4 +++- src/pack.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pack-objects.c b/src/pack-objects.c index 500104c55..7f427e3bd 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -505,8 +505,10 @@ static git_pobject **compute_write_order(git_packbuilder *pb) /* * Mark objects that are at the tip of tags. */ - if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) + if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) { + git__free(wo); return NULL; + } /* * Give the objects in the original recency order until diff --git a/src/pack.c b/src/pack.c index 7ce7099e0..497db38e8 100644 --- a/src/pack.c +++ b/src/pack.c @@ -329,8 +329,10 @@ static int pack_index_open(struct git_pack_file *p) memcpy(idx_name, p->pack_name, base_len); memcpy(idx_name + base_len, ".idx", sizeof(".idx")); - if ((error = git_mutex_lock(&p->lock)) < 0) + if ((error = git_mutex_lock(&p->lock)) < 0) { + git__free(idx_name); return error; + } if (p->index_version == -1) error = pack_index_check(idx_name, p); From c6451624c4631ccf039d0f3d5af6fbf4364f001c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Duraffort?= Date: Mon, 15 Jul 2013 16:00:07 +0200 Subject: [PATCH 4/4] Fix some more memory leaks in error path --- src/merge.c | 4 +++- src/odb.c | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/merge.c b/src/merge.c index 82d2e6f37..2e94ce1cd 100644 --- a/src/merge.c +++ b/src/merge.c @@ -1902,8 +1902,10 @@ static int write_merge_msg( entries = git__calloc(heads_len, sizeof(struct merge_msg_entry)); GITERR_CHECK_ALLOC(entries); - if (git_vector_init(&matching, heads_len, NULL) < 0) + if (git_vector_init(&matching, heads_len, NULL) < 0) { + git__free(entries); return -1; + } for (i = 0; i < heads_len; i++) entries[i].merge_head = heads[i]; diff --git a/src/odb.c b/src/odb.c index 8e62efd00..23eb4e12e 100644 --- a/src/odb.c +++ b/src/odb.c @@ -232,6 +232,7 @@ int git_odb__hashlink(git_oid *out, const char *path) link_data[size] = '\0'; if (read_len != (ssize_t)size) { giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", path); + git__free(link_data); return -1; }