Re: [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jul 17, 2020, 16:33 UTC
- Message-ID
- <xmqqzh7ydrt6.fsf@gitster.c.googlers.com>
- In-Reply-To
- <20200717105406.82226-4-mirucam@gmail.com>
Miriam Rubio <mirucam@gmail.com> writes:
Show 8 quoted lines
> + fp = fopen(path, mode);
> + if (!fp)
> + return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
> + res = vfprintf(fp, format, args);
> +
> + if (res < 0) {
> + fclose(fp);
> + return error_errno(_("could not write to file '%s'"), path);If the fclose(fp) failed, the errno from that failure (i.e. why fp cannot be closed) would be reported by the error_errno(), which may not be ideal. Either use just error() (which may be suboptimal), follow an often-used pattern to use save_errno (grep for the symbol to learn from the existing code) and keep using error_errno(), or check errors from fclose() too (which may be overkill).
if (res < 0) {
int saved_errno = errno;
#if OVERKILL
if (fclose(fp))
error_errno(_("..."));
#else
fclose(fp);
#endif
errno = saved_errno;
return error_errno(_("..."), ...);
}