config: close config file handle in case of error

When updating an existing configuration file, we did not always close the filehandle that is reading from the current configuration file when we encountered an error (e.g. when unsetting a variable that does not exist). Signed-off-by: Sven Strickroth <email@cs-ware.de> Signed-off-by: Sup Yut Sum <ch3cooli@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sven Strickroth committed Aug 14, 2015 at 22:21 UTC 54d160ec0deebfa7bf0cfc499c69ff5364b118c6
1 file changed +4 -1
config.c
+4 -1
@@ -1935,7 +1935,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
1935 const char *key, const char *value,
1936 const char *value_regex, int multi_replace)
1937 {
1938 - int fd = -1, in_fd;
1938 + int fd = -1, in_fd = -1;
1939 int ret;
1940 struct lock_file *lock = NULL;
1941 char *filename_buf = NULL;
@@ -2065,6 +2065,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
2065 goto out_free;
2066 }
2067 close(in_fd);
2068 + in_fd = -1;
2069
2070 if (chmod(lock->filename.buf, st.st_mode & 07777) < 0) {
2071 error("chmod on %s failed: %s",
@@ -2148,6 +2149,8 @@ out_free:
2149 free(filename_buf);
2150 if (contents)
2151 munmap(contents, contents_sz);
2152 + if (in_fd >= 0)
2153 + close(in_fd);
2154 return ret;
2155
2156 write_err_out: