Skip to content

Commit 96d14cb

Browse files
committed
Merge branch 'jk/config-lockfile-leak-fix' into maint
A leakfix. * jk/config-lockfile-leak-fix: config: use a static lock_file struct
2 parents f77196e + f991761 commit 96d14cb

1 file changed

Lines changed: 7 additions & 17 deletions

File tree

config.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
24042404
{
24052405
int fd = -1, in_fd = -1;
24062406
int ret;
2407-
struct lock_file *lock = NULL;
2407+
static struct lock_file lock;
24082408
char *filename_buf = NULL;
24092409
char *contents = NULL;
24102410
size_t contents_sz;
@@ -2423,8 +2423,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
24232423
* The lock serves a purpose in addition to locking: the new
24242424
* contents of .git/config will be written into it.
24252425
*/
2426-
lock = xcalloc(1, sizeof(struct lock_file));
2427-
fd = hold_lock_file_for_update(lock, config_filename, 0);
2426+
fd = hold_lock_file_for_update(&lock, config_filename, 0);
24282427
if (fd < 0) {
24292428
error_errno("could not lock config file %s", config_filename);
24302429
free(store.key);
@@ -2537,8 +2536,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
25372536
close(in_fd);
25382537
in_fd = -1;
25392538

2540-
if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
2541-
error_errno("chmod on %s failed", get_lock_file_path(lock));
2539+
if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
2540+
error_errno("chmod on %s failed", get_lock_file_path(&lock));
25422541
ret = CONFIG_NO_WRITE;
25432542
goto out_free;
25442543
}
@@ -2593,28 +2592,19 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
25932592
contents = NULL;
25942593
}
25952594

2596-
if (commit_lock_file(lock) < 0) {
2595+
if (commit_lock_file(&lock) < 0) {
25972596
error_errno("could not write config file %s", config_filename);
25982597
ret = CONFIG_NO_WRITE;
2599-
lock = NULL;
26002598
goto out_free;
26012599
}
26022600

2603-
/*
2604-
* lock is committed, so don't try to roll it back below.
2605-
* NOTE: Since lockfile.c keeps a linked list of all created
2606-
* lock_file structures, it isn't safe to free(lock). It's
2607-
* better to just leave it hanging around.
2608-
*/
2609-
lock = NULL;
26102601
ret = 0;
26112602

26122603
/* Invalidate the config cache */
26132604
git_config_clear();
26142605

26152606
out_free:
2616-
if (lock)
2617-
rollback_lock_file(lock);
2607+
rollback_lock_file(&lock);
26182608
free(filename_buf);
26192609
if (contents)
26202610
munmap(contents, contents_sz);
@@ -2623,7 +2613,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26232613
return ret;
26242614

26252615
write_err_out:
2626-
ret = write_error(get_lock_file_path(lock));
2616+
ret = write_error(get_lock_file_path(&lock));
26272617
goto out_free;
26282618

26292619
}

0 commit comments

Comments
 (0)