git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Jorge <griffin@gmx.es>, git@vger.kernel.org
Subject: [PATCH 2/4] config.c: fix mmap leak when writing config
Date: Thu, 28 May 2015 03:54:43 -0400	[thread overview]
Message-ID: <20150528075443.GB23395@peff.net> (raw)
In-Reply-To: <20150528075142.GB3688@peff.net>

We mmap the existing config file, but fail to unmap it if we
hit an error. The function already has a shared exit path,
so we can fix this by moving the mmap pointer to the
function scope and clearing it in the shared exit.

Signed-off-by: Jeff King <peff@peff.net>
---
 config.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/config.c b/config.c
index ab46462..6917100 100644
--- a/config.c
+++ b/config.c
@@ -1939,6 +1939,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
 	int ret;
 	struct lock_file *lock = NULL;
 	char *filename_buf = NULL;
+	char *contents = NULL;
+	size_t contents_sz;
 
 	/* parse-key returns negative; flip the sign to feed exit(3) */
 	ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
@@ -1988,8 +1990,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 			goto write_err_out;
 	} else {
 		struct stat st;
-		char *contents;
-		size_t contents_sz, copy_begin, copy_end;
+		size_t copy_begin, copy_end;
 		int i, new_line = 0;
 
 		if (value_regex == NULL)
@@ -2108,8 +2109,6 @@ int git_config_set_multivar_in_file(const char *config_filename,
 					  contents_sz - copy_begin) <
 			    contents_sz - copy_begin)
 				goto write_err_out;
-
-		munmap(contents, contents_sz);
 	}
 
 	if (commit_lock_file(lock) < 0) {
@@ -2135,6 +2134,8 @@ out_free:
 	if (lock)
 		rollback_lock_file(lock);
 	free(filename_buf);
+	if (contents)
+		munmap(contents, contents_sz);
 	return ret;
 
 write_err_out:
-- 
2.4.2.668.gc3b1ade.dirty

  parent reply	other threads:[~2015-05-28  7:54 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-27 13:29 Bug: .gitconfig folder Jorge
2015-05-27 20:30 ` Junio C Hamano
2015-05-27 22:18   ` Jeff King
2015-05-27 22:24     ` Stefan Beller
2015-05-28  6:13       ` Jeff King
2015-05-27 22:38     ` Junio C Hamano
2015-05-28  7:51       ` Jeff King
2015-05-28  7:54         ` [PATCH 1/4] read-cache.c: drop PROT_WRITE from mmap of index Jeff King
2015-05-28  7:54         ` Jeff King [this message]
2015-06-30 14:34           ` [PATCH] config.c: fix writing config files on Windows network shares Karsten Blees
2015-06-30 14:46             ` Torsten Bögershausen
2015-06-30 16:01               ` Jeff King
2015-06-30 14:52             ` Johannes Schindelin
2015-06-30 16:00             ` Jeff King
2015-05-28  7:56         ` [PATCH 3/4] config.c: avoid xmmap error messages Jeff King
2015-05-28  8:03         ` [PATCH 4/4] config.c: rewrite ENODEV into EISDIR when mmap fails Jeff King
2015-05-28 17:11           ` Junio C Hamano
2015-05-28 20:44             ` Jeff King
2015-05-28 21:11               ` Junio C Hamano
2015-05-28 17:06         ` Bug: .gitconfig folder 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

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

  git send-email \
    --in-reply-to=20150528075443.GB23395@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=griffin@gmx.es \
    /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.
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 NNTP newsgroup(s).