Commit a3e8b7cd3e49bdcffc9d9d5bda1d134a448932e2

Edward Thomson 2020-04-05T17:18:20

mwindow: use GIT_ASSERT

diff --git a/src/indexer.c b/src/indexer.c
index 1068a50..b6a2ee4 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -817,7 +817,8 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_inde
 	/* Now that we have data in the pack, let's try to parse it */
 
 	/* As the file grows any windows we try to use will be out of date */
-	git_mwindow_free_all(mwf);
+	if ((error = git_mwindow_free_all(mwf)) < 0)
+		goto on_error;
 
 	while (stats->indexed_objects < idx->nr_objects) {
 		if ((error = read_stream_object(idx, stats)) != 0) {
@@ -861,16 +862,16 @@ static int index_path(git_buf *path, git_indexer *idx, const char *suffix)
  * Rewind the packfile by the trailer, as we might need to fix the
  * packfile by injecting objects at the tail and must overwrite it.
  */
-static void seek_back_trailer(git_indexer *idx)
+static int seek_back_trailer(git_indexer *idx)
 {
 	idx->pack->mwf.size -= GIT_OID_RAWSZ;
-	git_mwindow_free_all(&idx->pack->mwf);
+	return git_mwindow_free_all(&idx->pack->mwf);
 }
 
 static int inject_object(git_indexer *idx, git_oid *id)
 {
-	git_odb_object *obj;
-	struct entry *entry;
+	git_odb_object *obj = NULL;
+	struct entry *entry = NULL;
 	struct git_pack_entry *pentry = NULL;
 	git_oid foo = {{0}};
 	unsigned char hdr[64];
@@ -880,12 +881,14 @@ static int inject_object(git_indexer *idx, git_oid *id)
 	size_t len, hdr_len;
 	int error;
 
-	seek_back_trailer(idx);
+	if ((error = seek_back_trailer(idx)) < 0)
+		goto cleanup;
+
 	entry_start = idx->pack->mwf.size;
 
-	if (git_odb_read(&obj, idx->odb, id) < 0) {
+	if ((error = git_odb_read(&obj, idx->odb, id)) < 0) {
 		git_error_set(GIT_ERROR_INDEXER, "missing delta bases");
-		return -1;
+		goto cleanup;
 	}
 
 	data = git_odb_object_data(obj);
@@ -1085,7 +1088,9 @@ static int update_header_and_rehash(git_indexer *idx, git_indexer_progress *stat
 	 * hash_partially() keep the existing trailer out of the
 	 * calculation.
 	 */
-	git_mwindow_free_all(mwf);
+	if (git_mwindow_free_all(mwf) < 0)
+		return -1;
+
 	idx->inbuf_len = 0;
 	while (hashed < mwf->size) {
 		ptr = git_mwindow_open(mwf, &w, hashed, chunk, &left);
@@ -1257,7 +1262,8 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
 	if (git_filebuf_commit_at(&index_file, filename.ptr) < 0)
 		goto on_error;
 
-	git_mwindow_free_all(&idx->pack->mwf);
+	if (git_mwindow_free_all(&idx->pack->mwf) < 0)
+		goto on_error;
 
 	/* Truncate file to undo rounding up to next page_size in append_to_pack */
 	if (p_ftruncate(idx->pack->mwf.fd, idx->pack->mwf.size) < 0) {
diff --git a/src/mwindow.c b/src/mwindow.c
index a852d6b..66fd218 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -52,7 +52,7 @@ int git_mwindow_global_init(void)
 {
 	int error;
 
-	assert(!git__pack_cache);
+	GIT_ASSERT(!git__pack_cache);
 
 	if ((error = git_mutex_init(&git__mwindow_mutex)) < 0 ||
 	    (error = git_strmap_new(&git__pack_cache)) < 0)
@@ -105,18 +105,18 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
 	return 0;
 }
 
-void git_mwindow_put_pack(struct git_pack_file *pack)
+int git_mwindow_put_pack(struct git_pack_file *pack)
 {
-	int count;
+	int count, error;
 
-	if (git_mutex_lock(&git__mwindow_mutex) < 0)
-		return;
+	if ((error = git_mutex_lock(&git__mwindow_mutex)) < 0)
+		return error;
 
 	/* put before get would be a corrupted state */
-	assert(git__pack_cache);
+	GIT_ASSERT(git__pack_cache);
 
 	/* if we cannot find it, the state is corrupted */
-	assert(git_strmap_exists(git__pack_cache, pack->pack_name));
+	GIT_ASSERT(git_strmap_exists(git__pack_cache, pack->pack_name));
 
 	count = git_atomic_dec(&pack->refcount);
 	if (count == 0) {
@@ -125,26 +125,30 @@ void git_mwindow_put_pack(struct git_pack_file *pack)
 	}
 
 	git_mutex_unlock(&git__mwindow_mutex);
-	return;
+	return 0;
 }
 
-void git_mwindow_free_all(git_mwindow_file *mwf)
+int git_mwindow_free_all(git_mwindow_file *mwf)
 {
+	int error;
+
 	if (git_mutex_lock(&git__mwindow_mutex)) {
 		git_error_set(GIT_ERROR_THREAD, "unable to lock mwindow mutex");
-		return;
+		return -1;
 	}
 
-	git_mwindow_free_all_locked(mwf);
+	error = git_mwindow_free_all_locked(mwf);
 
 	git_mutex_unlock(&git__mwindow_mutex);
+
+	return error;
 }
 
 /*
  * Free all the windows in a sequence, typically because we're done
  * with the file
  */
-void git_mwindow_free_all_locked(git_mwindow_file *mwf)
+int git_mwindow_free_all_locked(git_mwindow_file *mwf)
 {
 	git_mwindow_ctl *ctl = &git_mwindow__mem_ctl;
 	size_t i;
@@ -166,7 +170,7 @@ void git_mwindow_free_all_locked(git_mwindow_file *mwf)
 
 	while (mwf->windows) {
 		git_mwindow *w = mwf->windows;
-		assert(w->inuse_cnt == 0);
+		GIT_ASSERT(w->inuse_cnt == 0);
 
 		ctl->mapped -= w->window_map.len;
 		ctl->open_windows--;
@@ -176,6 +180,8 @@ void git_mwindow_free_all_locked(git_mwindow_file *mwf)
 		mwf->windows = w->next;
 		git__free(w);
 	}
+
+	return 0;
 }
 
 /*
@@ -210,8 +216,8 @@ static bool git_mwindow_scan_recently_used(
 	git_mwindow *lru_window = NULL, *lru_last = NULL;
 	bool found = false;
 
-	assert(mwf);
-	assert(out_window);
+	GIT_ASSERT_ARG(mwf);
+	GIT_ASSERT_ARG(out_window);
 
 	lru_window = *out_window;
 	if (out_last)
diff --git a/src/mwindow.h b/src/mwindow.h
index 7519fc3..b379fba 100644
--- a/src/mwindow.h
+++ b/src/mwindow.h
@@ -40,8 +40,8 @@ typedef struct git_mwindow_ctl {
 } git_mwindow_ctl;
 
 int git_mwindow_contains(git_mwindow *win, off64_t offset);
-void git_mwindow_free_all(git_mwindow_file *mwf); /* locks */
-void git_mwindow_free_all_locked(git_mwindow_file *mwf); /* run under lock */
+int git_mwindow_free_all(git_mwindow_file *mwf); /* locks */
+int git_mwindow_free_all_locked(git_mwindow_file *mwf); /* run under lock */
 unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, off64_t offset, size_t extra, unsigned int *left);
 int git_mwindow_file_register(git_mwindow_file *mwf);
 void git_mwindow_file_deregister(git_mwindow_file *mwf);
@@ -51,6 +51,6 @@ extern int git_mwindow_global_init(void);
 
 struct git_pack_file; /* just declaration to avoid cyclical includes */
 int git_mwindow_get_pack(struct git_pack_file **out, const char *path);
-void git_mwindow_put_pack(struct git_pack_file *pack);
+int git_mwindow_put_pack(struct git_pack_file *pack);
 
 #endif