Merge branch 'rs/misc-cppcheck-fixes'

Various small fixes.

* rs/misc-cppcheck-fixes:
  server-info: avoid calling fclose(3) twice in update_info_file()
  files_for_each_reflog_ent_reverse(): close stream and free strbuf on error
  am: close stream on error, but not stdin
This commit is contained in:
Junio C Hamano 2017-04-23 22:07:56 -07:00
commit f9096db54b
3 changed files with 23 additions and 13 deletions

View file

@ -762,14 +762,18 @@ static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1); mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
out = fopen(mail, "w"); out = fopen(mail, "w");
if (!out) if (!out) {
if (in != stdin)
fclose(in);
return error_errno(_("could not open '%s' for writing"), return error_errno(_("could not open '%s' for writing"),
mail); mail);
}
ret = fn(out, in, keep_cr); ret = fn(out, in, keep_cr);
fclose(out); fclose(out);
fclose(in); if (in != stdin)
fclose(in);
if (ret) if (ret)
return error(_("could not parse patch '%s'"), *paths); return error(_("could not parse patch '%s'"), *paths);

View file

@ -3294,8 +3294,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
/* Jump to the end */ /* Jump to the end */
if (fseek(logfp, 0, SEEK_END) < 0) if (fseek(logfp, 0, SEEK_END) < 0)
return error("cannot seek back reflog for %s: %s", ret = error("cannot seek back reflog for %s: %s",
refname, strerror(errno)); refname, strerror(errno));
pos = ftell(logfp); pos = ftell(logfp);
while (!ret && 0 < pos) { while (!ret && 0 < pos) {
int cnt; int cnt;
@ -3305,13 +3305,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
/* Fill next block from the end */ /* Fill next block from the end */
cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
if (fseek(logfp, pos - cnt, SEEK_SET)) if (fseek(logfp, pos - cnt, SEEK_SET)) {
return error("cannot seek back reflog for %s: %s", ret = error("cannot seek back reflog for %s: %s",
refname, strerror(errno)); refname, strerror(errno));
break;
}
nread = fread(buf, cnt, 1, logfp); nread = fread(buf, cnt, 1, logfp);
if (nread != 1) if (nread != 1) {
return error("cannot read %d bytes from reflog for %s: %s", ret = error("cannot read %d bytes from reflog for %s: %s",
cnt, refname, strerror(errno)); cnt, refname, strerror(errno));
break;
}
pos -= cnt; pos -= cnt;
scanp = endp = buf + cnt; scanp = endp = buf + cnt;

View file

@ -14,19 +14,21 @@ static int update_info_file(char *path, int (*generate)(FILE *))
char *tmp = mkpathdup("%s_XXXXXX", path); char *tmp = mkpathdup("%s_XXXXXX", path);
int ret = -1; int ret = -1;
int fd = -1; int fd = -1;
FILE *fp = NULL; FILE *fp = NULL, *to_close;
safe_create_leading_directories(path); safe_create_leading_directories(path);
fd = git_mkstemp_mode(tmp, 0666); fd = git_mkstemp_mode(tmp, 0666);
if (fd < 0) if (fd < 0)
goto out; goto out;
fp = fdopen(fd, "w"); to_close = fp = fdopen(fd, "w");
if (!fp) if (!fp)
goto out; goto out;
fd = -1;
ret = generate(fp); ret = generate(fp);
if (ret) if (ret)
goto out; goto out;
if (fclose(fp)) fp = NULL;
if (fclose(to_close))
goto out; goto out;
if (adjust_shared_perm(tmp) < 0) if (adjust_shared_perm(tmp) < 0)
goto out; goto out;