Commit cb43274a6b1eae490ab301f80ff13a69fe252610

Edward Thomson 2020-01-18T17:42:52

index functions: return an int Stop returning a void for functions, future-proofing them to allow them to fail.

diff --git a/include/git2/sys/index.h b/include/git2/sys/index.h
index f1900df..1f6d93f 100644
--- a/include/git2/sys/index.h
+++ b/include/git2/sys/index.h
@@ -75,8 +75,9 @@ GIT_EXTERN(int) git_index_name_add(git_index *index,
  * Remove all filename conflict entries.
  *
  * @param index an existing index object
+ * @return 0 or an error code
  */
-GIT_EXTERN(void) git_index_name_clear(git_index *index);
+GIT_EXTERN(int) git_index_name_clear(git_index *index);
 
 /**@}*/
 
@@ -170,8 +171,9 @@ GIT_EXTERN(int) git_index_reuc_remove(git_index *index, size_t n);
  * Remove all resolve undo entries from the index
  *
  * @param index an existing index object
+ * @return 0 or an error code
  */
-GIT_EXTERN(void) git_index_reuc_clear(git_index *index);
+GIT_EXTERN(int) git_index_reuc_clear(git_index *index);
 
 /**@}*/
 
diff --git a/src/index.c b/src/index.c
index e2a8c7b..907bd6d 100644
--- a/src/index.c
+++ b/src/index.c
@@ -539,13 +539,19 @@ int git_index_clear(git_index *index)
 	git_idxmap_clear(index->entries_map);
 	while (!error && index->entries.length > 0)
 		error = index_remove_entry(index, index->entries.length - 1);
+
+	if (error)
+		goto done;
+
 	index_free_deleted(index);
 
-	git_index_reuc_clear(index);
-	git_index_name_clear(index);
+	if ((error = git_index_name_clear(index)) < 0 ||
+		(error = git_index_reuc_clear(index)) < 0)
+	    goto done;
 
 	git_futils_filestamp_set(&index->stamp, NULL);
 
+done:
 	return error;
 }
 
@@ -2136,7 +2142,7 @@ int git_index_name_add(git_index *index,
 	return 0;
 }
 
-void git_index_name_clear(git_index *index)
+int git_index_name_clear(git_index *index)
 {
 	size_t i;
 	git_index_name_entry *conflict_name;
@@ -2149,6 +2155,8 @@ void git_index_name_clear(git_index *index)
 	git_vector_clear(&index->names);
 
 	index->dirty = 1;
+
+	return 0;
 }
 
 size_t git_index_reuc_entrycount(git_index *index)
@@ -2245,7 +2253,7 @@ int git_index_reuc_remove(git_index *index, size_t position)
 	return error;
 }
 
-void git_index_reuc_clear(git_index *index)
+int git_index_reuc_clear(git_index *index)
 {
 	size_t i;
 
@@ -2257,6 +2265,8 @@ void git_index_reuc_clear(git_index *index)
 	git_vector_clear(&index->reuc);
 
 	index->dirty = 1;
+
+	return 0;
 }
 
 static int index_error_invalid(const char *message)
@@ -3277,8 +3287,9 @@ static int git_index_read_iterator(
 		}
 	}
 
-	git_index_name_clear(index);
-	git_index_reuc_clear(index);
+	if ((error = git_index_name_clear(index)) < 0 ||
+		(error = git_index_reuc_clear(index)) < 0)
+	    goto done;
 
 	git_vector_swap(&new_entries, &index->entries);
 	new_entries_map = git__swap(index->entries_map, new_entries_map);