Skip to content

Commit 3a1b312

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a1293ef commit 3a1b312

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

config.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
19341934
int ret;
19351935
struct lock_file *lock = NULL;
19361936
char *filename_buf = NULL;
1937+
char *contents = NULL;
1938+
size_t contents_sz;
19371939

19381940
/* parse-key returns negative; flip the sign to feed exit(3) */
19391941
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,
19831985
goto write_err_out;
19841986
} else {
19851987
struct stat st;
1986-
char *contents;
1987-
size_t contents_sz, copy_begin, copy_end;
1988+
size_t copy_begin, copy_end;
19881989
int i, new_line = 0;
19891990

19901991
if (value_regex == NULL)
@@ -2103,8 +2104,6 @@ int git_config_set_multivar_in_file(const char *config_filename,
21032104
contents_sz - copy_begin) <
21042105
contents_sz - copy_begin)
21052106
goto write_err_out;
2106-
2107-
munmap(contents, contents_sz);
21082107
}
21092108

21102109
if (commit_lock_file(lock) < 0) {
@@ -2130,6 +2129,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
21302129
if (lock)
21312130
rollback_lock_file(lock);
21322131
free(filename_buf);
2132+
if (contents)
2133+
munmap(contents, contents_sz);
21332134
return ret;
21342135

21352136
write_err_out:

0 commit comments

Comments
 (0)