@matheusbsbs / matheus-bagliano / commits / da2a95b2a8
Fork 0

cleanups: Fix potential bugs in connect.c

The strncmp for ACK was ACK does not include the final space. Presumably either we should either remove the trailing space, or compare 4 chars (as this patch does). 'path' is sometimes strdup'ed, but never freed. Signed-off-by: Serge E. Hallyn <serue@us.ibm.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Serge E. Hallyn committed Apr 17, 2006 at 10:14 UTC da2a95b2a86e8b140d6c5413ea4307e395884135
1 file changed +14 -4
connect.c
+14 -4
@@ -74,7 +74,7 @@ int get_ack(int fd, unsigned char *result_sha1)
74 line[--len] = 0;
75 if (!strcmp(line, "NAK"))
76 return 0;
77 - if (!strncmp(line, "ACK ", 3)) {
77 + if (!strncmp(line, "ACK ", 4)) {
78 if (!get_sha1_hex(line+4, result_sha1)) {
79 if (strstr(line+45, "continue"))
80 return 2;
@@ -567,6 +567,7 @@ int git_connect(int fd[2], char *url, const char *prog)
567 int pipefd[2][2];
568 pid_t pid;
569 enum protocol protocol = PROTO_LOCAL;
570 + int free_path = 0;
571
572 host = strstr(url, "://");
573 if(host) {
@@ -610,16 +611,23 @@ int git_connect(int fd[2], char *url, const char *prog)
611 char *ptr = path;
612 if (path[1] == '~')
613 path++;
613 - else
614 + else {
615 path = strdup(ptr);
616 + free_path = 1;
617 + }
618
619 *ptr = '\0';
620 }
621
622 if (protocol == PROTO_GIT) {
623 + int ret;
624 if (git_use_proxy(host))
621 - return git_proxy_connect(fd, prog, host, path);
622 - return git_tcp_connect(fd, prog, host, path);
625 + ret = git_proxy_connect(fd, prog, host, path);
626 + else
627 + ret = git_tcp_connect(fd, prog, host, path);
628 + if (free_path)
629 + free(path);
630 + return ret;
631 }
632
633 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
@@ -659,6 +667,8 @@ int git_connect(int fd[2], char *url, const char *prog)
667 fd[1] = pipefd[1][1];
668 close(pipefd[0][1]);
669 close(pipefd[1][0]);
670 + if (free_path)
671 + free(path);
672 return pid;
673 }
674