From 63170bcae91e5ddf6c0a2589d7212e0e62c8b269 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 17:51:08 +0100 Subject: [PATCH 1/7] Fix a memory leak in `git_pathspec__vinit`. --- src/pathspec.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pathspec.c b/src/pathspec.c index bad8dacdb..d6ce09c02 100644 --- a/src/pathspec.c +++ b/src/pathspec.c @@ -89,8 +89,10 @@ int git_pathspec__vinit( if (ret == GIT_ENOTFOUND) { git__free(match); continue; - } else if (ret < 0) + } else if (ret < 0) { + git__free(match); return ret; + } if (git_vector_insert(vspec, match) < 0) return -1; From 1234738e06c806ebafaf0ec04523adc823999c2d Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 22:12:02 +0100 Subject: [PATCH 2/7] Fix a memory leak in `git_config_iterator_glob_new`. --- src/config.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config.c b/src/config.c index b8d78c23b..8447608cd 100644 --- a/src/config.c +++ b/src/config.c @@ -458,6 +458,7 @@ int git_config_iterator_glob_new(git_config_iterator **out, const git_config *cf if ((result = regcomp(&iter->regex, regexp, REG_EXTENDED)) < 0) { giterr_set_regex(&iter->regex, result); regfree(&iter->regex); + git__free(iter); return -1; } From a8e4cb11fd77d52529e1e464df52a6db1aae33e7 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 22:12:17 +0100 Subject: [PATCH 3/7] Fix a memory leak in `config_parse`. --- src/config_file.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/config_file.c b/src/config_file.c index 2e78f7c8b..c7727c029 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -1072,8 +1072,10 @@ static int config_parse(diskfile_backend *cfg_file, struct reader *reader, git_c git_buf_printf(&buf, "%s.%s", current_section, var_name); git__free(var_name); - if (git_buf_oom(&buf)) + if (git_buf_oom(&buf)) { + git__free(var_value); return -1; + } var->entry->name = git_buf_detach(&buf); var->entry->value = var_value; From ddf1b1ffa5e6cea27562aa56df3955b5a916828a Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 22:33:10 +0100 Subject: [PATCH 4/7] Fix a memory leak in `hash_and_save` and `inject_object`. --- src/indexer.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 6132571cc..4af503546 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -373,8 +373,10 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_star entry->crc = crc32(0L, Z_NULL, 0); entry_size = (size_t)(idx->off - entry_start); - if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) + if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) { + git__free(pentry); goto on_error; + } return save_entry(idx, entry, pentry, entry_start); @@ -648,8 +650,10 @@ static int inject_object(git_indexer *idx, git_oid *id) entry_start = seek_back_trailer(idx); - if (git_odb_read(&obj, idx->odb, id) < 0) + if (git_odb_read(&obj, idx->odb, id) < 0) { + git__free(entry); return -1; + } data = git_odb_object_data(obj); len = git_odb_object_size(obj); @@ -662,8 +666,10 @@ static int inject_object(git_indexer *idx, git_oid *id) idx->pack->mwf.size += hdr_len; entry->crc = crc32(entry->crc, hdr, hdr_len); - if ((error = git__compress(&buf, data, len)) < 0) + if ((error = git__compress(&buf, data, len)) < 0) { + git__free(entry); goto cleanup; + } /* And then the compressed object */ git_filebuf_write(&idx->pack_file, buf.ptr, buf.size); @@ -672,8 +678,10 @@ static int inject_object(git_indexer *idx, git_oid *id) git_buf_free(&buf); /* Write a fake trailer so the pack functions play ball */ - if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0) + if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0) { + git__free(entry); goto cleanup; + } idx->pack->mwf.size += GIT_OID_RAWSZ; From b0b32b432111adf33fa3c3cb3beee7a81087704b Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 22:51:10 +0100 Subject: [PATCH 5/7] Fix a double free issue in `git_blame__alloc`. `git_blame_free` already calls `git__free` on `gbr`. --- src/blame.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/blame.c b/src/blame.c index 45f55ed14..b885de214 100644 --- a/src/blame.c +++ b/src/blame.c @@ -121,7 +121,6 @@ git_blame* git_blame__alloc( git_vector_insert(&gbr->paths, git__strdup(path)) < 0) { git_blame_free(gbr); - git__free(gbr); return NULL; } From ac44b3d2447f06f4022bb4b58a160244e24c039c Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Mon, 13 Jan 2014 23:28:03 +0100 Subject: [PATCH 6/7] Incorporate @ethomson's suggestions. --- src/indexer.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 4af503546..6290ea6cc 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -355,7 +355,7 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_star git_oid oid; size_t entry_size; struct entry *entry; - struct git_pack_entry *pentry; + struct git_pack_entry *pentry = NULL; entry = git__calloc(1, sizeof(*entry)); GITERR_CHECK_ALLOC(entry); @@ -373,14 +373,13 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_star entry->crc = crc32(0L, Z_NULL, 0); entry_size = (size_t)(idx->off - entry_start); - if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) { - git__free(pentry); + if (crc_object(&entry->crc, &idx->pack->mwf, entry_start, entry_size) < 0) goto on_error; - } return save_entry(idx, entry, pentry, entry_start); on_error: + git__free(pentry); git__free(entry); git__free(obj->data); return -1; @@ -636,7 +635,7 @@ static int inject_object(git_indexer *idx, git_oid *id) { git_odb_object *obj; struct entry *entry; - struct git_pack_entry *pentry; + struct git_pack_entry *pentry = NULL; git_oid foo = {{0}}; unsigned char hdr[64]; git_buf buf = GIT_BUF_INIT; @@ -666,10 +665,8 @@ static int inject_object(git_indexer *idx, git_oid *id) idx->pack->mwf.size += hdr_len; entry->crc = crc32(entry->crc, hdr, hdr_len); - if ((error = git__compress(&buf, data, len)) < 0) { - git__free(entry); - goto cleanup; - } + if ((error = git__compress(&buf, data, len)) < 0) + goto error; /* And then the compressed object */ git_filebuf_write(&idx->pack_file, buf.ptr, buf.size); @@ -678,10 +675,8 @@ static int inject_object(git_indexer *idx, git_oid *id) git_buf_free(&buf); /* Write a fake trailer so the pack functions play ball */ - if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0) { - git__free(entry); - goto cleanup; - } + if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0) + goto error; idx->pack->mwf.size += GIT_OID_RAWSZ; @@ -692,10 +687,14 @@ static int inject_object(git_indexer *idx, git_oid *id) git_oid_cpy(&entry->oid, id); idx->off = entry_start + hdr_len + len; - if ((error = save_entry(idx, entry, pentry, entry_start)) < 0) - git__free(pentry); + if (!(error = save_entry(idx, entry, pentry, entry_start))) + goto done; -cleanup: +error: + git__free(entry); + git__free(pentry); + +done: git_odb_object_free(obj); return error; } From 249537573b8509fc8ed0ae8e51835d084af7c3d2 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Tue, 14 Jan 2014 19:08:58 +0100 Subject: [PATCH 7/7] Incorporate @arrbee's suggestions. --- src/indexer.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/indexer.c b/src/indexer.c index 6290ea6cc..bcca2eb6c 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -644,19 +644,17 @@ static int inject_object(git_indexer *idx, git_oid *id) size_t len, hdr_len; int error; - entry = git__calloc(1, sizeof(*entry)); - GITERR_CHECK_ALLOC(entry); - entry_start = seek_back_trailer(idx); - if (git_odb_read(&obj, idx->odb, id) < 0) { - git__free(entry); + if (git_odb_read(&obj, idx->odb, id) < 0) return -1; - } data = git_odb_object_data(obj); len = git_odb_object_size(obj); + entry = git__calloc(1, sizeof(*entry)); + GITERR_CHECK_ALLOC(entry); + entry->crc = crc32(0L, Z_NULL, 0); /* Write out the object header */ @@ -666,7 +664,7 @@ static int inject_object(git_indexer *idx, git_oid *id) entry->crc = crc32(entry->crc, hdr, hdr_len); if ((error = git__compress(&buf, data, len)) < 0) - goto error; + goto cleanup; /* And then the compressed object */ git_filebuf_write(&idx->pack_file, buf.ptr, buf.size); @@ -676,7 +674,7 @@ static int inject_object(git_indexer *idx, git_oid *id) /* Write a fake trailer so the pack functions play ball */ if ((error = git_filebuf_write(&idx->pack_file, &foo, GIT_OID_RAWSZ)) < 0) - goto error; + goto cleanup; idx->pack->mwf.size += GIT_OID_RAWSZ; @@ -687,14 +685,14 @@ static int inject_object(git_indexer *idx, git_oid *id) git_oid_cpy(&entry->oid, id); idx->off = entry_start + hdr_len + len; - if (!(error = save_entry(idx, entry, pentry, entry_start))) - goto done; + error = save_entry(idx, entry, pentry, entry_start); -error: - git__free(entry); - git__free(pentry); +cleanup: + if (error) { + git__free(entry); + git__free(pentry); + } -done: git_odb_object_free(obj); return error; }