dial_git(): fix memory leak and simplify Simplify dial_git() by formatting the initial Git protocol packet with dprintf, suggested by millert, and stop leaking an asprintf buffer. ok stsp
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
diff --git a/lib/fetch.c b/lib/fetch.c
index b581bbe..0c3e366 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -156,8 +156,8 @@ dial_git(int *fetchfd, const char *host, const char *port, const char *path,
{
const struct got_error *err = NULL;
struct addrinfo hints, *servinfo, *p;
- char *cmd = NULL, *pkt = NULL;
- int fd = -1, totlen, r, eaicode;
+ char *cmd = NULL;
+ int fd = -1, len, r, eaicode;
*fetchfd = -1;
@@ -193,28 +193,12 @@ dial_git(int *fetchfd, const char *host, const char *port, const char *path,
err = got_error_from_errno("asprintf");
goto done;
}
- totlen = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
- if (asprintf(&pkt, "%04x%s", totlen, cmd) == -1) {
- err = got_error_from_errno("asprintf");
- goto done;
- }
- r = write(fd, pkt, strlen(pkt) + 1);
- if (r == -1) {
- err = got_error_from_errno("write");
- goto done;
- }
- if (asprintf(&pkt, "host=%s", host) == -1) {
- err = got_error_from_errno("asprintf");
- goto done;
- }
- r = write(fd, pkt, strlen(pkt) + 1);
- if (r == -1) {
- err = got_error_from_errno("write");
- goto done;
- }
+ len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
+ r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
+ if (r < 0)
+ err = got_error_from_errno("dprintf");
done:
free(cmd);
- free(pkt);
if (err) {
if (fd != -1)
close(fd);