Commit 8934ea8b1b31166ea1f3f857e6f51f8dd6a999a2

Omar Polo 2022-02-14T13:14:36

factorize imsg_clear calls after imsg_flush failures imsg_clear frees and closes resources allocated as part of enqueueing imsgs so it's a no-op after reads. discussed with and ok stsp@

diff --git a/lib/fetch.c b/lib/fetch.c
index 66bbb44..e557ed1 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -476,7 +476,6 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 			if (err)
 				break;
 		}
-		imsg_clear(&idxibuf);
 	}
 	if (close(imsg_idxfds[0]) == -1) {
 		err = got_error_from_errno("close");
diff --git a/lib/gotconfig.c b/lib/gotconfig.c
index 664f170..ab88498 100644
--- a/lib/gotconfig.c
+++ b/lib/gotconfig.c
@@ -108,7 +108,6 @@ got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
 	if (err)
 		goto done;
 
-	imsg_clear(ibuf);
 	err = got_privsep_send_stop(imsg_fds[0]);
 	child_err = got_privsep_wait_for_child(pid);
 	if (child_err && err == NULL)
diff --git a/lib/pack.c b/lib/pack.c
index c981da3..b807943 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -727,7 +727,6 @@ pack_stop_privsep_child(struct got_pack *pack)
 	err = got_privsep_wait_for_child(pack->privsep_child->pid);
 	if (close(pack->privsep_child->imsg_fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
-	imsg_clear(pack->privsep_child->ibuf);
 	free(pack->privsep_child->ibuf);
 	free(pack->privsep_child);
 	pack->privsep_child = NULL;
diff --git a/lib/privsep.c b/lib/privsep.c
index 48e50b6..4cbc40e 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -208,6 +208,7 @@ got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
 	if (ret == -1) {
 		fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
 		    getprogname(), err->code, err->msg, strerror(errno));
+		imsg_clear(ibuf);
 		return;
 	}
 }
@@ -221,8 +222,10 @@ flush_imsg(struct imsgbuf *ibuf)
 	if (err)
 		return err;
 
-	if (imsg_flush(ibuf) == -1)
+	if (imsg_flush(ibuf) == -1) {
+		imsg_clear(ibuf);
 		return got_error_from_errno("imsg_flush");
+	}
 
 	return NULL;
 }
@@ -245,7 +248,6 @@ got_privsep_send_stop(int fd)
 		return got_error_from_errno("imsg_compose STOP");
 
 	err = flush_imsg(&ibuf);
-	imsg_clear(&ibuf);
 	return err;
 }
 
diff --git a/lib/repository.c b/lib/repository.c
index 255d035..5260575 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -577,7 +577,6 @@ parse_gitconfig_file(int *gitconfig_repository_format_version,
 			goto done;
 	}
 
-	imsg_clear(ibuf);
 	err = got_privsep_send_stop(imsg_fds[0]);
 	child_err = got_privsep_wait_for_child(pid);
 	if (child_err && err == NULL)
diff --git a/lib/repository_admin.c b/lib/repository_admin.c
index 422da92..c1adcc4 100644
--- a/lib/repository_admin.c
+++ b/lib/repository_admin.c
@@ -387,7 +387,6 @@ got_repo_index_pack(FILE *packfile, struct got_object_id *pack_hash,
 			if (err)
 				break;
 		}
-		imsg_clear(&idxibuf);
 	}
 	if (close(imsg_idxfds[0]) == -1) {
 		err = got_error_from_errno("close");