Commit 557bd1f4108272c6db004aa8b7137d9254c14945

Vicent Marti 2014-01-14T10:27:57

Merge pull request #2043 from arthurschreiber/arthur/fix-memory-leaks Fix a bunch of memory leaks.

diff --git a/src/blame.c b/src/blame.c
index 45f55ed..b885de2 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;
 	}
 
diff --git a/src/config.c b/src/config.c
index b8d78c2..8447608 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;
 	}
 
diff --git a/src/config_file.c b/src/config_file.c
index 2e78f7c..c7727c0 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;
diff --git a/src/indexer.c b/src/indexer.c
index ccab8fc..9b60ef4 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -353,7 +353,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);
@@ -377,6 +377,7 @@ static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_star
 	return save_entry(idx, entry, pentry, entry_start);
 
 on_error:
+	git__free(pentry);
 	git__free(entry);
 	git__free(obj->data);
 	return -1;
@@ -632,7 +633,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;
@@ -641,9 +642,6 @@ 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)
@@ -652,6 +650,9 @@ static int inject_object(git_indexer *idx, git_oid *id)
 	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 */
@@ -682,10 +683,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);
+	error = save_entry(idx, entry, pentry, entry_start);
 
 cleanup:
+	if (error) {
+		git__free(entry);
+		git__free(pentry);
+	}
+
 	git_odb_object_free(obj);
 	return error;
 }
diff --git a/src/pathspec.c b/src/pathspec.c
index bad8dac..d6ce09c 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;