{"thread":{"id":"35431","subject":"[PATCH v7 10/10] git_connect(): Use common return point","startedAt":"2013-11-28T19:50:15Z","lastAt":"2013-11-28T19:50:15Z","messageCount":1,"participants":["Torsten Bögershausen"],"isPatch":true,"patchVersion":7,"patchTotal":10},"messages":[{"id":"231261","messageId":"201311282050.16371.tboegi@web.de","threadId":"35431","inReplyTo":null,"subject":"[PATCH v7 10/10] git_connect(): Use common return point","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2013-11-28T19:50:15Z","receivedAt":"2013-11-28T19:50:15Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"Use only one return point from git_connect(), doing the\nfree():\nreturn conn;\nonly at one place in the code.\n\nThere may be a little confusion what the variable \"host\" is for.\nAt some places it is only the host part, at other places it may include the\nport number, so change host into hostandport here.\n\nSigned-off-by: Torsten Bögershausen <tboegi@web.de>\n---\n connect.c | 108 +++++++++++++++++++++++++++++---------------------------------\n 1 file changed, 50 insertions(+), 58 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 04093c4..8a013a7 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -656,7 +656,7 @@ static struct child_process no_fork;\n struct child_process *git_connect(int fd[2], const char *url,\n \t\t\t\t  const char *prog, int flags)\n {\n-\tchar *host, *path;\n+\tchar *hostandport, *path;\n \tstruct child_process *conn = &no_fork;\n \tenum protocol protocol;\n \tconst char **arg;\n@@ -667,26 +667,22 @@ struct child_process *git_connect(int fd[2], const char *url,\n \t */\n \tsignal(SIGCHLD, SIG_DFL);\n \n-\tprotocol = parse_connect_url(url, &host, &path);\n+\tprotocol = parse_connect_url(url, &hostandport, &path);\n \tif (flags & CONNECT_DIAG_URL) {\n \t\tprintf(\"Diag: url=%s\\n\", url ? url : \"NULL\");\n \t\tprintf(\"Diag: protocol=%s\\n\", prot_name(protocol));\n-\t\tprintf(\"Diag: hostandport=%s\\n\", host ? host : \"NULL\");\n+\t\tprintf(\"Diag: hostandport=%s\\n\", hostandport ? hostandport : \"NULL\");\n \t\tprintf(\"Diag: path=%s\\n\", path ? path : \"NULL\");\n-\t\tfree(host);\n-\t\tfree(path);\n-\t\treturn NULL;\n-\t}\n-\n-\tif (protocol == PROTO_GIT) {\n+\t\tconn = NULL;\n+\t} else if (protocol == PROTO_GIT) {\n \t\t/* These underlying connection commands die() if they\n \t\t * cannot connect.\n \t\t */\n-\t\tchar *target_host = xstrdup(host);\n-\t\tif (git_use_proxy(host))\n-\t\t\tconn = git_proxy_connect(fd, host);\n+\t\tchar *target_host = xstrdup(hostandport);\n+\t\tif (git_use_proxy(hostandport))\n+\t\t\tconn = git_proxy_connect(fd, hostandport);\n \t\telse\n-\t\t\tgit_tcp_connect(fd, host, flags);\n+\t\t\tgit_tcp_connect(fd, hostandport, flags);\n \t\t/*\n \t\t * Separate original protocol components prog and path\n \t\t * from extended host header with a NUL byte.\n@@ -699,54 +695,50 @@ struct child_process *git_connect(int fd[2], const char *url,\n \t\t\t     prog, path, 0,\n \t\t\t     target_host, 0);\n \t\tfree(target_host);\n-\t\tfree(host);\n-\t\tfree(path);\n-\t\treturn conn;\n-\t}\n-\n-\tconn = xcalloc(1, sizeof(*conn));\n-\n-\tstrbuf_addstr(&cmd, prog);\n-\tstrbuf_addch(&cmd, ' ');\n-\tsq_quote_buf(&cmd, path);\n-\n-\tconn->in = conn->out = -1;\n-\tconn->argv = arg = xcalloc(7, sizeof(*arg));\n-\tif (protocol == PROTO_SSH) {\n-\t\tconst char *ssh = getenv(\"GIT_SSH\");\n-\t\tint putty = ssh && strcasestr(ssh, \"plink\");\n-\t\tchar *ssh_host = host; /* keep host for the free() below */\n-\t\tconst char *port = NULL;\n-\t\tget_host_and_port(&ssh_host, &port);\n-\t\tport = get_port_numeric(port);\n-\n-\t\tif (!ssh) ssh = \"ssh\";\n-\n-\t\t*arg++ = ssh;\n-\t\tif (putty && !strcasestr(ssh, \"tortoiseplink\"))\n-\t\t\t*arg++ = \"-batch\";\n-\t\tif (port) {\n-\t\t\t/* P is for PuTTY, p is for OpenSSH */\n-\t\t\t*arg++ = putty ? \"-P\" : \"-p\";\n-\t\t\t*arg++ = port;\n+\t} else {\n+\t\tconn = xcalloc(1, sizeof(*conn));\n+\n+\t\tstrbuf_addstr(&cmd, prog);\n+\t\tstrbuf_addch(&cmd, ' ');\n+\t\tsq_quote_buf(&cmd, path);\n+\n+\t\tconn->in = conn->out = -1;\n+\t\tconn->argv = arg = xcalloc(7, sizeof(*arg));\n+\t\tif (protocol == PROTO_SSH) {\n+\t\t\tconst char *ssh = getenv(\"GIT_SSH\");\n+\t\t\tint putty = ssh && strcasestr(ssh, \"plink\");\n+\t\t\tchar *ssh_host = hostandport;\n+\t\t\tconst char *port = NULL;\n+\t\t\tget_host_and_port(&ssh_host, &port);\n+\t\t\tport = get_port_numeric(port);\n+\n+\t\t\tif (!ssh) ssh = \"ssh\";\n+\n+\t\t\t*arg++ = ssh;\n+\t\t\tif (putty && !strcasestr(ssh, \"tortoiseplink\"))\n+\t\t\t\t*arg++ = \"-batch\";\n+\t\t\tif (port) {\n+\t\t\t\t/* P is for PuTTY, p is for OpenSSH */\n+\t\t\t\t*arg++ = putty ? \"-P\" : \"-p\";\n+\t\t\t\t*arg++ = port;\n+\t\t\t}\n+\t\t\t*arg++ = ssh_host;\n+\t\t}\telse {\n+\t\t\t/* remove repo-local variables from the environment */\n+\t\t\tconn->env = local_repo_env;\n+\t\t\tconn->use_shell = 1;\n \t\t}\n-\t\t*arg++ = ssh_host;\n-\t}\n-\telse {\n-\t\t/* remove repo-local variables from the environment */\n-\t\tconn->env = local_repo_env;\n-\t\tconn->use_shell = 1;\n-\t}\n-\t*arg++ = cmd.buf;\n-\t*arg = NULL;\n+\t\t*arg++ = cmd.buf;\n+\t\t*arg = NULL;\n \n-\tif (start_command(conn))\n-\t\tdie(\"unable to fork\");\n+\t\tif (start_command(conn))\n+\t\t\tdie(\"unable to fork\");\n \n-\tfd[0] = conn->out; /* read from child's stdout */\n-\tfd[1] = conn->in;  /* write to child's stdin */\n-\tstrbuf_release(&cmd);\n-\tfree(host);\n+\t\tfd[0] = conn->out; /* read from child's stdout */\n+\t\tfd[1] = conn->in;  /* write to child's stdin */\n+\t\tstrbuf_release(&cmd);\n+\t}\n+\tfree(hostandport);\n \tfree(path);\n \treturn conn;\n }\n-- \n1.8.5.rc0.23.gaa27064\n"}]}