connect: move no_fork fallback to git_tcp_connect

git_connect has the structure struct child_process *conn = &no_fork; ... switch (protocol) { case PROTO_GIT: if (git_use_proxy(hostandport)) conn = git_proxy_connect(fd, hostandport); else git_tcp_connect(fd, hostandport, flags); ... break; case PROTO_SSH: conn = xmalloc(sizeof(*conn)); child_process_init(conn); argv_array_push(&conn->args, ssh); ... break; ... return conn; In all cases except the git_tcp_connect case, conn is explicitly assigned a value. Make the code clearer by explicitly assigning 'conn = &no_fork' in the tcp case and eliminating the default so the compiler can ensure conn is always correctly assigned. Noticed-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Nov 20, 2017 at 13:22 UTC 8e349780ecb9bcec52c7df22fcbfb4afd0d7936c
1 file changed +21 -15
connect.c
+21 -15
@@ -582,12 +582,25 @@ static int git_tcp_connect_sock(char *host, int flags)
582 #endif /* NO_IPV6 */
583
584
585 -static void git_tcp_connect(int fd[2], char *host, int flags)
585 +/*
586 + * Dummy child_process returned by git_connect() if the transport protocol
587 + * does not need fork(2).
588 + */
589 +static struct child_process no_fork = CHILD_PROCESS_INIT;
590 +
591 +int git_connection_is_socket(struct child_process *conn)
592 +{
593 + return conn == &no_fork;
594 +}
595 +
596 +static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
597 {
598 int sockfd = git_tcp_connect_sock(host, flags);
599
600 fd[0] = sockfd;
601 fd[1] = dup(sockfd);
602 +
603 + return &no_fork;
604 }
605
606
@@ -761,8 +774,6 @@ static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
774 return protocol;
775 }
776
764 -static struct child_process no_fork = CHILD_PROCESS_INIT;
765 -
777 static const char *get_ssh_command(void)
778 {
779 const char *ssh;
@@ -851,11 +862,11 @@ static enum ssh_variant determine_ssh_variant(const char *ssh_command,
862 }
863
864 /*
854 - * This returns a dummy child_process if the transport protocol does not
855 - * need fork(2), or a struct child_process object if it does. Once done,
856 - * finish the connection with finish_connect() with the value returned from
857 - * this function (it is safe to call finish_connect() with NULL to support
858 - * the former case).
865 + * This returns the dummy child_process `no_fork` if the transport protocol
866 + * does not need fork(2), or a struct child_process object if it does. Once
867 + * done, finish the connection with finish_connect() with the value returned
868 + * from this function (it is safe to call finish_connect() with NULL to
869 + * support the former case).
870 *
871 * If it returns, the connect is successful; it just dies on errors (this
872 * will hopefully be changed in a libification effort, to return NULL when
@@ -865,7 +876,7 @@ struct child_process *git_connect(int fd[2], const char *url,
876 const char *prog, int flags)
877 {
878 char *hostandport, *path;
868 - struct child_process *conn = &no_fork;
879 + struct child_process *conn;
880 enum protocol protocol;
881
882 /* Without this we cannot rely on waitpid() to tell
@@ -901,7 +912,7 @@ struct child_process *git_connect(int fd[2], const char *url,
912 if (git_use_proxy(hostandport))
913 conn = git_proxy_connect(fd, hostandport);
914 else
904 - git_tcp_connect(fd, hostandport, flags);
915 + conn = git_tcp_connect(fd, hostandport, flags);
916 /*
917 * Separate original protocol components prog and path
918 * from extended host header with a NUL byte.
@@ -1041,11 +1052,6 @@ struct child_process *git_connect(int fd[2], const char *url,
1052 return conn;
1053 }
1054
1044 -int git_connection_is_socket(struct child_process *conn)
1045 -{
1046 - return conn == &no_fork;
1047 -}
1048 -
1055 int finish_connect(struct child_process *conn)
1056 {
1057 int code;