avoid unnecessary imsg flushing in got_privsep_send_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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 *