Commit db72980323d3000411c18790d96556719c54a44f

Edward Thomson 2021-08-30T09:24:48

Merge pull request #6018 from libgit2/ethomson/fixups Fixes from code analysis

diff --git a/src/date.c b/src/date.c
index 71bf631..2297ee6 100644
--- a/src/date.c
+++ b/src/date.c
@@ -722,7 +722,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm 
 	while (tl->type) {
 		size_t len = strlen(tl->type);
 		if (match_string(date, tl->type) >= len-1) {
-			update_tm(tm, now, tl->length * *num);
+			update_tm(tm, now, tl->length * (unsigned long)*num);
 			*num = 0;
 			*touched = 1;
 			return end;
diff --git a/src/errors.c b/src/errors.c
index 3d1d1c9..ce883b2 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -107,11 +107,6 @@ int git_error_set_str(int error_class, const char *string)
 
 	GIT_ASSERT_ARG(string);
 
-	if (!string) {
-		git_error_set(GIT_ERROR_INVALID, "unspecified caller error");
-		return -1;
-	}
-
 	git_buf_clear(buf);
 	git_buf_puts(buf, string);
 
diff --git a/src/filter.c b/src/filter.c
index f1d9614..dd7d2f7 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -206,7 +206,8 @@ int git_filter_global_init(void)
 			GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0)
 		error = -1;
 
-	error = git_runtime_shutdown_register(git_filter_global_shutdown);
+	if (!error)
+		error = git_runtime_shutdown_register(git_filter_global_shutdown);
 
 done:
 	if (error) {
diff --git a/src/hashsig.c b/src/hashsig.c
index 43310ca..6b4fb83 100644
--- a/src/hashsig.c
+++ b/src/hashsig.c
@@ -286,8 +286,10 @@ int git_hashsig_create_fromfile(
 		return fd;
 	}
 
-	if ((error = hashsig_in_progress_init(&prog, sig)) < 0)
+	if ((error = hashsig_in_progress_init(&prog, sig)) < 0) {
+		p_close(fd);
 		return error;
+	}
 
 	while (!error) {
 		if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) {
diff --git a/src/midx.c b/src/midx.c
index 9aab8b5..6a885ed 100644
--- a/src/midx.c
+++ b/src/midx.c
@@ -714,8 +714,10 @@ static int midx_write(
 	error = git_vector_init(&object_entries, git_array_size(object_entries_array), object_entry__cmp);
 	if (error < 0)
 		goto cleanup;
-	git_array_foreach (object_entries_array, i, entry)
-		error = git_vector_set(NULL, &object_entries, i, entry);
+	git_array_foreach (object_entries_array, i, entry) {
+		if ((error = git_vector_set(NULL, &object_entries, i, entry)) < 0)
+			goto cleanup;
+	}
 	git_vector_set_sorted(&object_entries, 0);
 	git_vector_sort(&object_entries);
 	git_vector_uniq(&object_entries, NULL);
@@ -751,10 +753,12 @@ static int midx_write(
 			goto cleanup;
 		if (entry->offset >= 0x80000000l) {
 			word = htonl(0x80000000u | object_large_offsets_count++);
-			error = write_offset(entry->offset, midx_write_buf, &object_large_offsets);
+			if ((error = write_offset(entry->offset, midx_write_buf, &object_large_offsets)) < 0)
+				goto cleanup;
 		} else {
 			word = htonl((uint32_t)entry->offset & 0x7fffffffu);
 		}
+
 		error = git_buf_put(&object_offsets, (const char *)&word, sizeof(word));
 		if (error < 0)
 			goto cleanup;
diff --git a/src/pack.c b/src/pack.c
index 94b1ecd..aadf3f2 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -1298,7 +1298,12 @@ int git_pack_foreach_entry(
 		return error;
 	}
 
-	GIT_ASSERT(p->index_map.data);
+	if (!p->index_map.data) {
+		git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL");
+		git_mutex_unlock(&p->lock);
+		return -1;
+	}
+
 	index = p->index_map.data;
 
 	if (p->index_version > 1)
@@ -1387,7 +1392,11 @@ int git_pack_foreach_entry_offset(
 		if ((error = pack_index_open_locked(p)) < 0)
 			goto cleanup;
 
-		GIT_ASSERT(p->index_map.data);
+		if (!p->index_map.data) {
+			git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL");
+			goto cleanup;
+		}
+
 		index = p->index_map.data;
 	}
 
@@ -1479,7 +1488,11 @@ static int pack_entry_find_offset(
 	if ((error = pack_index_open_locked(p)) < 0)
 		goto cleanup;
 
-	GIT_ASSERT(p->index_map.data);
+	if (!p->index_map.data) {
+		git_error_set(GIT_ERROR_INTERNAL, "internal error: p->index_map.data == NULL");
+		goto cleanup;
+	}
+
 	index = p->index_map.data;
 	level1_ofs = p->index_map.data;
 
diff --git a/src/transports/httpclient.c b/src/transports/httpclient.c
index 4612b43..d40c162 100644
--- a/src/transports/httpclient.c
+++ b/src/transports/httpclient.c
@@ -598,7 +598,6 @@ static int apply_credentials(
 	} else if (!token.size) {
 		git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challenge");
 		error = GIT_EAUTH;
-		error = -1;
 		goto done;
 	}