diff options
author | Martin Ågren <martin.agren@gmail.com> | 2017-10-05 22:32:04 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-10-06 10:07:17 +0900 |
commit | 837e34eba47f209a38fc9ab458bd103fd7515325 (patch) | |
tree | ab91a9de784eec03c56b431b51dd4649270cfd9c /config.c | |
parent | f132a127eebb8f1b14b87fc317ca68278fceb2e8 (diff) | |
download | git-837e34eba47f209a38fc9ab458bd103fd7515325.tar.gz |
treewide: prefer lockfiles on the stack
There is no longer any need to allocate and leak a `struct lock_file`.
The previous patch addressed an instance where we needed a minor tweak
alongside the trivial changes.
Deal with the remaining instances where we allocate and leak a struct
within a single function. Change them to have the `struct lock_file` on
the stack instead.
These instances were identified by running `git grep "^\s*struct
lock_file\s*\*"`.
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -2748,7 +2748,7 @@ int git_config_rename_section_in_file(const char *config_filename, { int ret = 0, remove = 0; char *filename_buf = NULL; - struct lock_file *lock; + struct lock_file lock = LOCK_INIT; int out_fd; char buf[1024]; FILE *config_file = NULL; @@ -2762,8 +2762,7 @@ int git_config_rename_section_in_file(const char *config_filename, if (!config_filename) config_filename = filename_buf = git_pathdup("config"); - lock = xcalloc(1, sizeof(struct lock_file)); - out_fd = hold_lock_file_for_update(lock, config_filename, 0); + out_fd = hold_lock_file_for_update(&lock, config_filename, 0); if (out_fd < 0) { ret = error("could not lock config file %s", config_filename); goto out; @@ -2782,9 +2781,9 @@ int git_config_rename_section_in_file(const char *config_filename, goto out; } - if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { + if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) { ret = error_errno("chmod on %s failed", - get_lock_file_path(lock)); + get_lock_file_path(&lock)); goto out; } @@ -2805,7 +2804,7 @@ int git_config_rename_section_in_file(const char *config_filename, } store.baselen = strlen(new_name); if (write_section(out_fd, new_name) < 0) { - ret = write_error(get_lock_file_path(lock)); + ret = write_error(get_lock_file_path(&lock)); goto out; } /* @@ -2831,20 +2830,20 @@ int git_config_rename_section_in_file(const char *config_filename, continue; length = strlen(output); if (write_in_full(out_fd, output, length) < 0) { - ret = write_error(get_lock_file_path(lock)); + ret = write_error(get_lock_file_path(&lock)); goto out; } } fclose(config_file); config_file = NULL; commit_and_out: - if (commit_lock_file(lock) < 0) + if (commit_lock_file(&lock) < 0) ret = error_errno("could not write config file %s", config_filename); out: if (config_file) fclose(config_file); - rollback_lock_file(lock); + rollback_lock_file(&lock); out_no_rollback: free(filename_buf); return ret; |