config.c: fix mmap leak when writing config

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 28, 2015 at 03:54 UTC 3a1b3126ed5a0b51d5b1fdba827c92bf2acf5fc6
1 file changed +5 -4
config.c
+5 -4
@@ -1934,6 +1934,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
1934 int ret;
1935 struct lock_file *lock = NULL;
1936 char *filename_buf = NULL;
1937 + char *contents = NULL;
1938 + size_t contents_sz;
1939
1940 /* parse-key returns negative; flip the sign to feed exit(3) */
1941 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
@@ -1983,8 +1985,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
1985 goto write_err_out;
1986 } else {
1987 struct stat st;
1986 - char *contents;
1987 - size_t contents_sz, copy_begin, copy_end;
1988 + size_t copy_begin, copy_end;
1989 int i, new_line = 0;
1990
1991 if (value_regex == NULL)
@@ -2103,8 +2104,6 @@ int git_config_set_multivar_in_file(const char *config_filename,
2104 contents_sz - copy_begin) <
2105 contents_sz - copy_begin)
2106 goto write_err_out;
2106 -
2107 - munmap(contents, contents_sz);
2107 }
2108
2109 if (commit_lock_file(lock) < 0) {
@@ -2130,6 +2129,8 @@ out_free:
2129 if (lock)
2130 rollback_lock_file(lock);
2131 free(filename_buf);
2132 + if (contents)
2133 + munmap(contents, contents_sz);
2134 return ret;
2135
2136 write_err_out: