diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-04-23 22:07:56 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-04-23 22:07:56 -0700 |
commit | f9096db54b29ed17afc6e3393f159ea55142b14c (patch) | |
tree | 250cf5594f21f55a19c00b16559a9c3a221013b0 | |
parent | a507115e29690e831fedb060cfb50cef9e0f7514 (diff) | |
parent | fa1912c89a72fbd94591f4f5d522e5867ffe9bb6 (diff) | |
download | git-f9096db54b29ed17afc6e3393f159ea55142b14c.tar.gz git-f9096db54b29ed17afc6e3393f159ea55142b14c.tar.xz |
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
-rw-r--r-- | builtin/am.c | 8 | ||||
-rw-r--r-- | refs/files-backend.c | 20 | ||||
-rw-r--r-- | server-info.c | 8 |
3 files changed, 23 insertions, 13 deletions
diff --git a/builtin/am.c b/builtin/am.c index f7a7a971f..805f56cec 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -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); out = fopen(mail, "w"); - if (!out) + if (!out) { + if (in != stdin) + fclose(in); return error_errno(_("could not open '%s' for writing"), mail); + } ret = fn(out, in, keep_cr); fclose(out); - fclose(in); + if (in != stdin) + fclose(in); if (ret) return error(_("could not parse patch '%s'"), *paths); diff --git a/refs/files-backend.c b/refs/files-backend.c index 4d705b403..c9d900fd1 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3294,8 +3294,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store, /* Jump to the end */ if (fseek(logfp, 0, SEEK_END) < 0) - return error("cannot seek back reflog for %s: %s", - refname, strerror(errno)); + ret = error("cannot seek back reflog for %s: %s", + refname, strerror(errno)); pos = ftell(logfp); while (!ret && 0 < pos) { 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 */ cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; - if (fseek(logfp, pos - cnt, SEEK_SET)) - return error("cannot seek back reflog for %s: %s", - refname, strerror(errno)); + if (fseek(logfp, pos - cnt, SEEK_SET)) { + ret = error("cannot seek back reflog for %s: %s", + refname, strerror(errno)); + break; + } nread = fread(buf, cnt, 1, logfp); - if (nread != 1) - return error("cannot read %d bytes from reflog for %s: %s", - cnt, refname, strerror(errno)); + if (nread != 1) { + ret = error("cannot read %d bytes from reflog for %s: %s", + cnt, refname, strerror(errno)); + break; + } pos -= cnt; scanp = endp = buf + cnt; diff --git a/server-info.c b/server-info.c index 7bc4e75d2..f6c1a3dfb 100644 --- a/server-info.c +++ b/server-info.c @@ -14,19 +14,21 @@ static int update_info_file(char *path, int (*generate)(FILE *)) char *tmp = mkpathdup("%s_XXXXXX", path); int ret = -1; int fd = -1; - FILE *fp = NULL; + FILE *fp = NULL, *to_close; safe_create_leading_directories(path); fd = git_mkstemp_mode(tmp, 0666); if (fd < 0) goto out; - fp = fdopen(fd, "w"); + to_close = fp = fdopen(fd, "w"); if (!fp) goto out; + fd = -1; ret = generate(fp); if (ret) goto out; - if (fclose(fp)) + fp = NULL; + if (fclose(to_close)) goto out; if (adjust_shared_perm(tmp) < 0) goto out; |