Commit b00c982167ffc56b444ff3211a2b5a62c56ec589

Stefan Sperling 2018-11-04T21:37:28

avoid unnecessary imsg flushing in got_privsep_send_tree()

diff --git a/lib/privsep.c b/lib/privsep.c
index 325eec8..24bc13c 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -626,16 +626,14 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 	const struct got_error *err = NULL;
 	struct got_imsg_tree_object itree;
 	struct got_tree_entry *te;
+	size_t totlen;
 
 	itree.nentries = tree->entries.nentries;
 	if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
 	    == -1)
 		return got_error_from_errno();
 
-	err = flush_imsg(ibuf);
-	if (err)
-		return err;
-
+	totlen = sizeof(itree);
 	SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
 		struct got_imsg_tree_entry ite;
 		uint8_t *buf = NULL;
@@ -644,6 +642,12 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 		if (len > MAX_IMSGSIZE)
 			return got_error(GOT_ERR_NO_SPACE);
 
+		if (totlen + len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
+			err = flush_imsg(ibuf);
+			if (err)
+				return err;
+		}
+
 		buf = malloc(len);
 		if (buf == NULL)
 			return got_error_from_errno();
@@ -659,13 +663,10 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 		free(buf);
 		if (err)
 			return err;
-
-		err = flush_imsg(ibuf);
-		if (err)
-			return err;
+		totlen += len;
 	}
 
-	return NULL;
+	return flush_imsg(ibuf);
 }
 
 const struct got_error *