@matheusbsbs / matheus-bagliano / commits / fc1905bb93
Fork 0

config_rename_section: fix FILE* leak

Noticed by SungHyun Nam. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 19, 2006 at 21:55 UTC fc1905bb9340304fb5980841fca24638028c1c5e
1 file changed +13 -6
config.c
+13 -6
@@ -749,7 +749,7 @@ out_free:
749 int git_config_rename_section(const char *old_name, const char *new_name)
750 {
751 int ret = 0;
752 - const char *config_filename;
752 + char *config_filename;
753 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
754 int out_fd;
755 char buf[1024];
@@ -762,11 +762,15 @@ int git_config_rename_section(const char *old_name, const char *new_name)
762 }
763 config_filename = xstrdup(config_filename);
764 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
765 - if (out_fd < 0)
766 - return error("Could not lock config file!");
765 + if (out_fd < 0) {
766 + ret = error("Could not lock config file!");
767 + goto out;
768 + }
769
768 - if (!(config_file = fopen(config_filename, "rb")))
769 - return error("Could not open config file!");
770 + if (!(config_file = fopen(config_filename, "rb"))) {
771 + ret = error("Could not open config file!");
772 + goto out;
773 + }
774
775 while (fgets(buf, sizeof(buf), config_file)) {
776 int i;
@@ -806,8 +810,11 @@ int git_config_rename_section(const char *old_name, const char *new_name)
810 }
811 write(out_fd, buf, strlen(buf));
812 }
813 + fclose(config_file);
814 if (close(out_fd) || commit_lock_file(lock) < 0)
810 - return error("Cannot commit config file!");
815 + ret = error("Cannot commit config file!");
816 + out:
817 + free(config_filename);
818 return ret;
819 }
820