Commit 13da562a873f0c68533e97a2990b891ac97d7f0a

Carlos Martín Nieto 2014-12-30T16:48:52

Merge pull request #2785 from jacquesg/coverity Coverity fixes

diff --git a/src/attr_file.c b/src/attr_file.c
index b3efeef..5b008b0 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -781,8 +781,10 @@ int git_attr_assignment__parse(
 
 					error = git_vector_insert_sorted(
 						assigns, massign, &merge_assignments);
-					if (error < 0 && error != GIT_EEXISTS)
+					if (error < 0 && error != GIT_EEXISTS) {
+						git_attr_assignment__free(assign);
 						return error;
+					}
 				}
 			}
 		}
diff --git a/src/config_file.c b/src/config_file.c
index 94f292c..4f041e7 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1228,13 +1228,6 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
 			if (result < 0)
 				break;
 
-			var = git__malloc(sizeof(cvar_t));
-			GITERR_CHECK_ALLOC(var);
-			memset(var, 0x0, sizeof(cvar_t));
-			var->entry = git__malloc(sizeof(git_config_entry));
-			GITERR_CHECK_ALLOC(var->entry);
-			memset(var->entry, 0x0, sizeof(git_config_entry));
-
 			git__strtolower(var_name);
 			git_buf_printf(&buf, "%s.%s", current_section, var_name);
 			git__free(var_name);
@@ -1244,6 +1237,11 @@ static int config_parse(git_strmap *values, diskfile_backend *cfg_file, struct r
 				return -1;
 			}
 
+			var = git__calloc(1, sizeof(cvar_t));
+			GITERR_CHECK_ALLOC(var);
+			var->entry = git__calloc(1, sizeof(git_config_entry));
+			GITERR_CHECK_ALLOC(var->entry);
+
 			var->entry->name = git_buf_detach(&buf);
 			var->entry->value = var_value;
 			var->entry->level = level;
diff --git a/src/crlf.c b/src/crlf.c
index 9344876..c0a7399 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -345,6 +345,8 @@ static void crlf_cleanup(
 git_filter *git_crlf_filter_new(void)
 {
 	struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter));
+	if (f == NULL)
+		return NULL;
 
 	f->f.version = GIT_FILTER_VERSION;
 	f->f.attributes = "crlf eol text";
diff --git a/src/describe.c b/src/describe.c
index 57f715b..d4c0dea 100644
--- a/src/describe.c
+++ b/src/describe.c
@@ -262,6 +262,7 @@ struct possible_tag {
 static int possible_tag_dup(struct possible_tag **out, struct possible_tag *in)
 {
 	struct possible_tag *tag;
+	int error;
 
 	tag = git__malloc(sizeof(struct possible_tag));
 	GITERR_CHECK_ALLOC(tag);
@@ -269,8 +270,11 @@ static int possible_tag_dup(struct possible_tag **out, struct possible_tag *in)
 	memcpy(tag, in, sizeof(struct possible_tag));
 	tag->name = NULL;
 
-	if (commit_name_dup(&tag->name, in->name) < 0)
-		return -1;
+	if ((error = commit_name_dup(&tag->name, in->name)) < 0) {
+		git__free(tag);
+		*out = NULL;
+		return error;
+	}
 
 	*out = tag;
 	return 0;
diff --git a/src/diff.c b/src/diff.c
index 1f3ee63..e23d389 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1555,6 +1555,7 @@ int git_diff_format_email(
 		if ((offset = (loc - opts->summary)) == 0) {
 			giterr_set(GITERR_INVALID, "summary is empty");
 			error = -1;
+			goto on_error;
 		}
 
 		summary = git__calloc(offset + 1, sizeof(char));
diff --git a/src/ident.c b/src/ident.c
index 5163087..6bc80ab 100644
--- a/src/ident.c
+++ b/src/ident.c
@@ -115,6 +115,8 @@ static int ident_apply(
 git_filter *git_ident_filter_new(void)
 {
 	git_filter *f = git__calloc(1, sizeof(git_filter));
+	if (f == NULL)
+		return NULL;
 
 	f->version = GIT_FILTER_VERSION;
 	f->attributes = "+ident"; /* apply to files with ident attribute set */
diff --git a/src/merge.c b/src/merge.c
index 1c55e79..7031efc 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1550,8 +1550,10 @@ git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo)
 	if (git_vector_init(&diff_list->staged, 0, NULL) < 0 ||
 		git_vector_init(&diff_list->conflicts, 0, NULL) < 0 ||
 		git_vector_init(&diff_list->resolved, 0, NULL) < 0 ||
-		git_pool_init(&diff_list->pool, 1, 0) < 0)
+		git_pool_init(&diff_list->pool, 1, 0) < 0) {
+		git_merge_diff_list__free(diff_list);
 		return NULL;
+	}
 
 	return diff_list;
 }
diff --git a/src/pack.c b/src/pack.c
index df25631..47ce854 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -158,6 +158,7 @@ static int cache_add(git_pack_cache *cache, git_rawobj *base, git_off_t offset)
 	if (entry) {
 		if (git_mutex_lock(&cache->lock) < 0) {
 			giterr_set(GITERR_OS, "failed to lock cache");
+			git__free(entry);
 			return -1;
 		}
 		/* Add it to the cache if nobody else has */
diff --git a/src/rebase.c b/src/rebase.c
index 2681b3e..ceb74d3 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -246,7 +246,8 @@ int git_rebase_open(git_rebase **out, git_repository *repo)
 
 	if (rebase->type == GIT_REBASE_TYPE_NONE) {
 		giterr_set(GITERR_REBASE, "There is no rebase in progress");
-		return GIT_ENOTFOUND;
+		error = GIT_ENOTFOUND;
+		goto done;
 	}
 
 	if ((error = git_buf_puts(&path, rebase->state_path)) < 0)
@@ -591,7 +592,8 @@ static int rebase_init(
 	git_buf state_path = GIT_BUF_INIT;
 	int error;
 
-	git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR);
+	if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0)
+		return error;
 
 	rebase->repo = repo;
 	rebase->type = GIT_REBASE_TYPE_MERGE;
diff --git a/src/repository.c b/src/repository.c
index 74a966e..0cf8eb6 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -656,7 +656,8 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
 		git_buf odb_path = GIT_BUF_INIT;
 		git_odb *odb;
 
-		git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR);
+		if ((error = git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR)) < 0)
+			return error;
 
 		error = git_odb_open(&odb, odb_path.ptr);
 		if (!error) {
@@ -741,7 +742,8 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo)
 		git_buf index_path = GIT_BUF_INIT;
 		git_index *index;
 
-		git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE);
+		if ((error = git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE)) < 0)
+			return error;
 
 		error = git_index_open(&index, index_path.ptr);
 		if (!error) {
@@ -2068,7 +2070,9 @@ int git_repository_is_shallow(git_repository *repo)
 	struct stat st;
 	int error;
 
-	git_buf_joinpath(&path, repo->path_repository, "shallow");
+	if ((error = git_buf_joinpath(&path, repo->path_repository, "shallow")) < 0)
+		return error;
+
 	error = git_path_lstat(path.ptr, &st);
 	git_buf_free(&path);
 
diff --git a/src/transports/local.c b/src/transports/local.c
index d698e01..c01755e 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -129,8 +129,10 @@ static int add_ref(transport_local *t, const char *name)
 	head = git__calloc(1, sizeof(git_remote_head));
 	GITERR_CHECK_ALLOC(head);
 
-	if (git_buf_join(&buf, 0, name, peeled) < 0)
+	if (git_buf_join(&buf, 0, name, peeled) < 0) {
+		free_head(head);
 		return -1;
+	}
 	head->name = git_buf_detach(&buf);
 
 	if (!(error = git_tag_peel(&target, (git_tag *)obj))) {
@@ -699,6 +701,7 @@ static void local_free(git_transport *transport)
 
 int git_transport_local(git_transport **out, git_remote *owner, void *param)
 {
+	int error;
 	transport_local *t;
 
 	GIT_UNUSED(param);
@@ -719,7 +722,11 @@ int git_transport_local(git_transport **out, git_remote *owner, void *param)
 	t->parent.read_flags = local_read_flags;
 	t->parent.cancel = local_cancel;
 
-	git_vector_init(&t->refs, 0, NULL);
+	if ((error = git_vector_init(&t->refs, 0, NULL)) < 0) {
+		git__free(t);
+		return error;
+	}
+
 	t->owner = owner;
 
 	*out = (git_transport *) t;
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index e110da0..5f1b998 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -82,7 +82,7 @@ static int append_symref(const char **out, git_vector *symrefs, const char *ptr)
 	int error;
 	const char *end;
 	git_buf buf = GIT_BUF_INIT;
-	git_refspec *mapping;
+	git_refspec *mapping = NULL;
 
 	ptr += strlen(GIT_CAP_SYMREF);
 	if (*ptr != '=')
@@ -97,7 +97,7 @@ static int append_symref(const char **out, git_vector *symrefs, const char *ptr)
 		return error;
 
 	/* symref mapping has refspec format */
-	mapping = git__malloc(sizeof(git_refspec));
+	mapping = git__calloc(1, sizeof(git_refspec));
 	GITERR_CHECK_ALLOC(mapping);
 
 	error = git_refspec__parse(mapping, git_buf_cstr(&buf), true);
@@ -119,6 +119,7 @@ static int append_symref(const char **out, git_vector *symrefs, const char *ptr)
 
 on_invalid:
 	giterr_set(GITERR_NET, "remote sent invalid symref");
+	git_refspec__free(mapping);
 	return -1;
 }
 
@@ -258,7 +259,7 @@ static int store_common(transport_smart *t)
 
 static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 {
-	git_revwalk *walk;
+	git_revwalk *walk = NULL;
 	git_strarray refs;
 	unsigned int i;
 	git_reference *ref;
@@ -294,6 +295,7 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 	return 0;
 
 on_error:
+	git_revwalk_free(walk);
 	git_reference_free(ref);
 	git_strarray_free(&refs);
 	return error;