Merge branch 'nd/config-misc-fixes'

Leakage of lockfiles in the config subsystem has been fixed.

* nd/config-misc-fixes:
  config.c: handle lock file in error case in git_config_rename_...
  config.c: rename label unlock_and_out
  config.c: handle error case for fstat() calls
This commit is contained in:
Junio C Hamano 2017-01-10 15:24:27 -08:00
commit 42087233c3

View File

@ -2216,7 +2216,12 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
goto out_free; goto out_free;
} }
fstat(in_fd, &st); if (fstat(in_fd, &st) == -1) {
error_errno(_("fstat on %s failed"), config_filename);
ret = CONFIG_INVALID_FILE;
goto out_free;
}
contents_sz = xsize_t(st.st_size); contents_sz = xsize_t(st.st_size);
contents = xmmap_gently(NULL, contents_sz, PROT_READ, contents = xmmap_gently(NULL, contents_sz, PROT_READ,
MAP_PRIVATE, in_fd, 0); MAP_PRIVATE, in_fd, 0);
@ -2418,7 +2423,7 @@ int git_config_rename_section_in_file(const char *config_filename,
if (new_name && !section_name_is_ok(new_name)) { if (new_name && !section_name_is_ok(new_name)) {
ret = error("invalid section name: %s", new_name); ret = error("invalid section name: %s", new_name);
goto out; goto out_no_rollback;
} }
if (!config_filename) if (!config_filename)
@ -2433,10 +2438,13 @@ int git_config_rename_section_in_file(const char *config_filename,
if (!(config_file = fopen(config_filename, "rb"))) { if (!(config_file = fopen(config_filename, "rb"))) {
/* no config file means nothing to rename, no error */ /* no config file means nothing to rename, no error */
goto unlock_and_out; goto commit_and_out;
} }
fstat(fileno(config_file), &st); if (fstat(fileno(config_file), &st) == -1) {
ret = error_errno(_("fstat on %s failed"), 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", ret = error_errno("chmod on %s failed",
@ -2492,11 +2500,13 @@ int git_config_rename_section_in_file(const char *config_filename,
} }
} }
fclose(config_file); fclose(config_file);
unlock_and_out: commit_and_out:
if (commit_lock_file(lock) < 0) if (commit_lock_file(lock) < 0)
ret = error_errno("could not write config file %s", ret = error_errno("could not write config file %s",
config_filename); config_filename);
out: out:
rollback_lock_file(lock);
out_no_rollback:
free(filename_buf); free(filename_buf);
return ret; return ret;
} }