Commit 7f358e3b0ea7c7873900dbce1501c56711d7275d

Stefan Sperling 2019-11-23T21:03:29

eliminate redundant memcpy in got_privsep_send_tree_req()

diff --git a/lib/privsep.c b/lib/privsep.c
index 0de0608..8d611b1 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -269,26 +269,30 @@ got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
     struct got_object_id *id, int pack_idx)
 {
 	const struct got_error *err = NULL;
-	struct got_imsg_packed_object iobj, *iobjp;
-	size_t len;
+	struct ibuf *wbuf;
+	size_t len = id ? sizeof(struct got_imsg_packed_object) : 0;
+
+	wbuf = imsg_create(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, len);
+	if (wbuf == NULL)
+		return got_error_from_errno("imsg_create TREE_REQUEST");
 
 	if (id) { /* tree is packed */
-		iobj.idx = pack_idx;
-		memcpy(iobj.id, id->sha1, sizeof(iobj.id));
-		iobjp = &iobj;
-		len = sizeof(iobj);
-	} else {
-		iobjp = NULL;
-		len = 0;
-	}
+		if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
+			err = got_error_from_errno("imsg_add TREE_ENTRY");
+			ibuf_free(wbuf);
+			return err;
+		}
 
-	if (imsg_compose(ibuf, GOT_IMSG_TREE_REQUEST, 0, 0, fd, iobjp, len)
-	    == -1) {
-		err = got_error_from_errno("imsg_compose TREE_REQUEST");
-		close(fd);
-		return err;
+		if (imsg_add(wbuf, &pack_idx, sizeof(pack_idx)) == -1) {
+			err = got_error_from_errno("imsg_add TREE_ENTRY");
+			ibuf_free(wbuf);
+			return err;
+		}
 	}
 
+	wbuf->fd = fd;
+	imsg_close(ibuf, wbuf);
+
 	return flush_imsg(ibuf);
 }