git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Rene Scharfe <l.s.r@web.de>
To: git@vger.kernel.org
Subject: [PATCH 08/34] connect: release strbuf on error return in git_connect()
Date: Wed, 30 Aug 2017 20:00:23 +0200	[thread overview]
Message-ID: <20170830180037.20950-1-l.s.r@web.de> (raw)
In-Reply-To: <20170830175005.20756-1-l.s.r@web.de>

Reduce the scope of the variable cmd and release it before returning
early.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 connect.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/connect.c b/connect.c
index 49b28b83be..df56c0cbff 100644
--- a/connect.c
+++ b/connect.c
@@ -775,146 +775,148 @@ static void handle_ssh_variant(const char *ssh_command, int is_cmdline,
 struct child_process *git_connect(int fd[2], const char *url,
 				  const char *prog, int flags)
 {
 	char *hostandport, *path;
 	struct child_process *conn = &no_fork;
 	enum protocol protocol;
-	struct strbuf cmd = STRBUF_INIT;
 
 	/* Without this we cannot rely on waitpid() to tell
 	 * what happened to our children.
 	 */
 	signal(SIGCHLD, SIG_DFL);
 
 	protocol = parse_connect_url(url, &hostandport, &path);
 	if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
 		printf("Diag: url=%s\n", url ? url : "NULL");
 		printf("Diag: protocol=%s\n", prot_name(protocol));
 		printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
 		printf("Diag: path=%s\n", path ? path : "NULL");
 		conn = NULL;
 	} else if (protocol == PROTO_GIT) {
 		/*
 		 * Set up virtual host information based on where we will
 		 * connect, unless the user has overridden us in
 		 * the environment.
 		 */
 		char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
 		if (target_host)
 			target_host = xstrdup(target_host);
 		else
 			target_host = xstrdup(hostandport);
 
 		transport_check_allowed("git");
 
 		/* These underlying connection commands die() if they
 		 * cannot connect.
 		 */
 		if (git_use_proxy(hostandport))
 			conn = git_proxy_connect(fd, hostandport);
 		else
 			git_tcp_connect(fd, hostandport, flags);
 		/*
 		 * Separate original protocol components prog and path
 		 * from extended host header with a NUL byte.
 		 *
 		 * Note: Do not add any other headers here!  Doing so
 		 * will cause older git-daemon servers to crash.
 		 */
 		packet_write_fmt(fd[1],
 			     "%s %s%chost=%s%c",
 			     prog, path, 0,
 			     target_host, 0);
 		free(target_host);
 	} else {
+		struct strbuf cmd = STRBUF_INIT;
+
 		conn = xmalloc(sizeof(*conn));
 		child_process_init(conn);
 
 		if (looks_like_command_line_option(path))
 			die("strange pathname '%s' blocked", path);
 
 		strbuf_addstr(&cmd, prog);
 		strbuf_addch(&cmd, ' ');
 		sq_quote_buf(&cmd, path);
 
 		/* remove repo-local variables from the environment */
 		conn->env = local_repo_env;
 		conn->use_shell = 1;
 		conn->in = conn->out = -1;
 		if (protocol == PROTO_SSH) {
 			const char *ssh;
 			int needs_batch = 0;
 			int port_option = 'p';
 			char *ssh_host = hostandport;
 			const char *port = NULL;
 			transport_check_allowed("ssh");
 			get_host_and_port(&ssh_host, &port);
 
 			if (!port)
 				port = get_port(ssh_host);
 
 			if (flags & CONNECT_DIAG_URL) {
 				printf("Diag: url=%s\n", url ? url : "NULL");
 				printf("Diag: protocol=%s\n", prot_name(protocol));
 				printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
 				printf("Diag: port=%s\n", port ? port : "NONE");
 				printf("Diag: path=%s\n", path ? path : "NULL");
 
 				free(hostandport);
 				free(path);
 				free(conn);
+				strbuf_release(&cmd);
 				return NULL;
 			}
 
 			if (looks_like_command_line_option(ssh_host))
 				die("strange hostname '%s' blocked", ssh_host);
 
 			ssh = get_ssh_command();
 			if (ssh)
 				handle_ssh_variant(ssh, 1, &port_option,
 						   &needs_batch);
 			else {
 				/*
 				 * GIT_SSH is the no-shell version of
 				 * GIT_SSH_COMMAND (and must remain so for
 				 * historical compatibility).
 				 */
 				conn->use_shell = 0;
 
 				ssh = getenv("GIT_SSH");
 				if (!ssh)
 					ssh = "ssh";
 				else
 					handle_ssh_variant(ssh, 0,
 							   &port_option,
 							   &needs_batch);
 			}
 
 			argv_array_push(&conn->args, ssh);
 			if (flags & CONNECT_IPV4)
 				argv_array_push(&conn->args, "-4");
 			else if (flags & CONNECT_IPV6)
 				argv_array_push(&conn->args, "-6");
 			if (needs_batch)
 				argv_array_push(&conn->args, "-batch");
 			if (port) {
 				argv_array_pushf(&conn->args,
 						 "-%c", port_option);
 				argv_array_push(&conn->args, port);
 			}
 			argv_array_push(&conn->args, ssh_host);
 		} else {
 			transport_check_allowed("file");
 		}
 		argv_array_push(&conn->args, cmd.buf);
 
 		if (start_command(conn))
 			die("unable to fork");
 
 		fd[0] = conn->out; /* read from child's stdout */
 		fd[1] = conn->in;  /* write to child's stdin */
 		strbuf_release(&cmd);
 	}
 	free(hostandport);
 	free(path);
 	return conn;
 }
-- 
2.14.1


  parent reply	other threads:[~2017-08-30 18:00 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-30 17:49 [PATCH 00/34] plug strbuf memory leaks Rene Scharfe
2017-08-30 17:49 ` [PATCH 01/34] am: release strbufs after use in detect_patch_format() Rene Scharfe
2017-08-31 17:31   ` Stefan Beller
2017-08-30 17:49 ` [PATCH 02/34] am: release strbuf on error return in hg_patch_to_mail() Rene Scharfe
2017-08-30 17:49 ` [PATCH 03/34] am: release strbuf after use in safe_to_abort() Rene Scharfe
2017-08-30 17:49 ` [PATCH 04/34] check-ref-format: release strbuf after use in check_ref_format_branch() Rene Scharfe
2017-08-30 17:49 ` [PATCH 05/34] clean: release strbuf after use in remove_dirs() Rene Scharfe
2017-08-30 17:49 ` [PATCH 06/34] clone: release strbuf after use in remove_junk() Rene Scharfe
2017-09-06 19:51   ` Junio C Hamano
2017-09-10  6:27     ` René Scharfe
2017-09-10  7:30       ` Jeff King
2017-09-10 10:37         ` René Scharfe
2017-09-10 17:38           ` Jeff King
2017-09-11 21:40             ` René Scharfe
2017-09-13 12:56               ` Jeff King
2017-08-30 17:49 ` [PATCH 07/34] commit: release strbuf on error return in commit_tree_extended() Rene Scharfe
2017-08-31 17:40   ` Stefan Beller
2017-08-30 17:49 ` [PATCH 08/34] connect: release strbuf on error return in git_connect() Rene Scharfe
2017-08-31 17:44   ` Stefan Beller
2017-08-30 17:49 ` [PATCH 09/34] convert: release strbuf on error return in filter_buffer_or_fd() Rene Scharfe
2017-08-30 17:49 ` [PATCH 10/34] diff: release strbuf after use in diff_summary() Rene Scharfe
2017-08-31 17:46   ` Stefan Beller
2017-08-30 17:49 ` [PATCH 11/34] diff: release strbuf after use in show_rename_copy() Rene Scharfe
2017-08-30 17:49 ` [PATCH 12/34] diff: release strbuf after use in show_stats() Rene Scharfe
2017-08-30 17:49 ` [PATCH 13/34] help: release strbuf on error return in exec_man_konqueror() Rene Scharfe
2017-08-30 17:49 ` [PATCH 14/34] help: release strbuf on error return in exec_man_man() Rene Scharfe
2017-08-30 17:49 ` [PATCH 15/34] help: release strbuf on error return in exec_woman_emacs() Rene Scharfe
2017-08-30 17:49 ` [PATCH 16/34] mailinfo: release strbuf after use in handle_from() Rene Scharfe
2017-08-30 17:49 ` [PATCH 17/34] mailinfo: release strbuf on error return in handle_boundary() Rene Scharfe
2017-08-30 18:23   ` Martin Ågren
2017-08-31 17:21     ` René Scharfe
2017-09-05 17:10       ` Martin Ågren
2017-08-30 17:49 ` [PATCH 18/34] merge: release strbuf after use in save_state() Rene Scharfe
2017-08-30 17:49 ` [PATCH 19/34] merge: release strbuf after use in write_merge_heads() Rene Scharfe
2017-08-30 17:57 ` [PATCH 20/34] notes: release strbuf after use in notes_copy_from_stdin() Rene Scharfe
2017-08-30 17:58 ` [PATCH 02/34] am: release strbuf on error return in hg_patch_to_mail() Rene Scharfe
2017-08-30 17:58   ` [PATCH 03/34] am: release strbuf after use in safe_to_abort() Rene Scharfe
2017-08-30 17:58   ` [PATCH 04/34] check-ref-format: release strbuf after use in check_ref_format_branch() Rene Scharfe
2017-08-30 17:58   ` [PATCH 08/34] connect: release strbuf on error return in git_connect() Rene Scharfe
2017-08-30 17:58   ` [PATCH 09/34] convert: release strbuf on error return in filter_buffer_or_fd() Rene Scharfe
2017-08-30 17:58   ` [PATCH 11/34] diff: release strbuf after use in show_rename_copy() Rene Scharfe
2017-08-30 17:58   ` [PATCH 12/34] diff: release strbuf after use in show_stats() Rene Scharfe
2017-08-30 17:58   ` [PATCH 21/34] refs: release strbuf on error return in write_pseudoref() Rene Scharfe
2017-08-30 18:00 ` Rene Scharfe [this message]
2017-08-30 18:00   ` Rene Scharfe
2017-08-30 18:00   ` [PATCH 22/34] remote: release strbuf after use in read_remote_branches() Rene Scharfe
2017-08-30 18:00   ` [PATCH 23/34] remote: release strbuf after use in migrate_file() Rene Scharfe
2017-08-30 18:00   ` [PATCH 24/34] remote: release strbuf after use in set_url() Rene Scharfe
2017-08-30 18:00   ` [PATCH 25/34] send-pack: release strbuf on error return in send_pack() Rene Scharfe
2017-08-30 18:00   ` [PATCH 26/34] sha1_file: release strbuf on error return in index_path() Rene Scharfe
2017-08-30 18:00   ` [PATCH 27/34] shortlog: release strbuf after use in insert_one_record() Rene Scharfe
2017-09-06 19:51     ` Junio C Hamano
2017-09-07  4:33       ` Jeff King
2017-09-08  0:33         ` Junio C Hamano
2017-09-08  3:56           ` Jeff King
2017-09-08  4:36             ` Jeff King
2017-09-08  6:39               ` Junio C Hamano
2017-09-08  9:21                 ` [PATCH] shortlog: skip format/parse roundtrip for internal traversal Jeff King
2017-09-10  8:44                   ` René Scharfe
2017-09-10  8:50                     ` Jeff King
2017-08-30 18:05 ` [PATCH 08/34] connect: release strbuf on error return in git_connect() Rene Scharfe
2017-08-30 18:20 ` [PATCH 21/34] refs: release strbuf on error return in write_pseudoref() Rene Scharfe
2017-08-30 18:20 ` [PATCH 25/34] send-pack: release strbuf on error return in send_pack() Rene Scharfe
2017-08-30 18:20 ` [PATCH 28/34] sequencer: release strbuf after use in save_head() Rene Scharfe
2017-08-30 18:20 ` [PATCH 29/34] transport-helper: release strbuf after use in process_connect_service() Rene Scharfe
2017-08-30 18:20 ` [PATCH 30/34] userdiff: release strbuf after use in userdiff_get_textconv() Rene Scharfe
2017-08-30 18:20 ` [PATCH 31/34] utf8: release strbuf on error return in strbuf_utf8_replace() Rene Scharfe
2017-08-30 18:20 ` [PATCH 33/34] wt-status: release strbuf after use in read_rebase_todolist() Rene Scharfe
2017-08-30 18:20 ` [PATCH 32/34] vcs-svn: release strbuf after use in end_revision() Rene Scharfe
2017-08-30 18:20 ` [PATCH 34/34] wt-status: release strbuf after use in wt_longstatus_print_tracking() Rene Scharfe
2017-09-06 19:51   ` Junio C Hamano
2017-09-10  6:27     ` René Scharfe
2017-09-10  7:39       ` Junio C Hamano
2017-08-31 18:05 ` [PATCH 00/34] plug strbuf memory leaks Stefan Beller
2017-09-06 19:51 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170830180037.20950-1-l.s.r@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).