Commit da6720fcc5d9f368e7aa562384439498c1bf307e

Edward Thomson 2015-06-10T09:02:54

Merge pull request #3201 from libgit2/cmn/coverity A few more fixes from coverity

diff --git a/src/filter.c b/src/filter.c
index c88fdd4..3c6a0a9 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -887,7 +887,7 @@ int git_filter_list_stream_file(
 	git_vector filter_streams = GIT_VECTOR_INIT;
 	git_writestream *stream_start;
 	ssize_t readlen;
-	int fd, error;
+	int fd = -1, error;
 
 	if ((error = stream_list_init(
 			&stream_start, &filter_streams, filters, target)) < 0 ||
@@ -909,9 +909,10 @@ int git_filter_list_stream_file(
 	else if (readlen < 0)
 		error = readlen;
 
-	p_close(fd);
 
 done:
+	if (fd >= 0)
+		p_close(fd);
 	stream_list_free(&filter_streams);
 	git_buf_free(&abspath);
 	return error;
diff --git a/src/merge.c b/src/merge.c
index 28fca00..517d317 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1143,9 +1143,9 @@ static void merge_diff_list_count_candidates(
 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry) &&
 			(!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->our_entry) ||
 			!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->their_entry)))
-			src_count++;
+			(*src_count)++;
 		else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry))
-			tgt_count++;
+			(*tgt_count)++;
 	}
 }
 
diff --git a/src/object.c b/src/object.c
index 1073559..a178e0e 100644
--- a/src/object.c
+++ b/src/object.c
@@ -129,10 +129,10 @@ int git_object_lookup_prefix(
 	if (error < 0)
 		return error;
 
-	if (len > GIT_OID_HEXSZ)
-		len = GIT_OID_HEXSZ;
+	if (len > GIT_OID_RAWSZ)
+		len = GIT_OID_RAWSZ;
 
-	if (len == GIT_OID_HEXSZ) {
+	if (len == GIT_OID_RAWSZ) {
 		git_cached_obj *cached = NULL;
 
 		/* We want to match the full id : we can first look up in the cache,
@@ -172,9 +172,9 @@ int git_object_lookup_prefix(
 		memcpy(short_oid.id, id->id, (len + 1) / 2);
 		if (len % 2)
 			short_oid.id[len / 2] &= 0xF0;
-		memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_HEXSZ - len) / 2);
+		memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_RAWSZ - len) / 2);
 
-		/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
+		/* If len < GIT_OID_RAWSZ (a strict short oid was given), we have
 		 * 2 options :
 		 * - We always search in the cache first. If we find that short oid is
 		 *	ambiguous, we can stop. But in all the other cases, we must then
diff --git a/src/pack.c b/src/pack.c
index 105d675..cd65267 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -319,9 +319,9 @@ static int pack_index_check(const char *path, struct git_pack_file *p)
 
 static int pack_index_open(struct git_pack_file *p)
 {
-	char *idx_name;
 	int error = 0;
-	size_t name_len, base_len;
+	size_t name_len;
+	git_buf idx_name = GIT_BUF_INIT;
 
 	if (p->index_version > -1)
 		return 0;
@@ -329,22 +329,23 @@ static int pack_index_open(struct git_pack_file *p)
 	name_len = strlen(p->pack_name);
 	assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
 
-	if ((idx_name = git__malloc(name_len)) == NULL)
+	git_buf_grow(&idx_name, name_len);
+	git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
+	git_buf_puts(&idx_name, ".idx");
+	if (git_buf_oom(&idx_name)) {
+		giterr_set_oom();
 		return -1;
-
-	base_len = name_len - strlen(".pack");
-	memcpy(idx_name, p->pack_name, base_len);
-	memcpy(idx_name + base_len, ".idx", sizeof(".idx"));
+	}
 
 	if ((error = git_mutex_lock(&p->lock)) < 0) {
-		git__free(idx_name);
+		git_buf_free(&idx_name);
 		return error;
 	}
 
 	if (p->index_version == -1)
-		error = pack_index_check(idx_name, p);
+		error = pack_index_check(idx_name.ptr, p);
 
-	git__free(idx_name);
+	git_buf_free(&idx_name);
 
 	git_mutex_unlock(&p->lock);