Skip to content

Commit f9096db

Browse files
committed
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
2 parents a507115 + fa1912c commit f9096db

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

builtin/am.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,14 +762,18 @@ static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
762762
mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
763763

764764
out = fopen(mail, "w");
765-
if (!out)
765+
if (!out) {
766+
if (in != stdin)
767+
fclose(in);
766768
return error_errno(_("could not open '%s' for writing"),
767769
mail);
770+
}
768771

769772
ret = fn(out, in, keep_cr);
770773

771774
fclose(out);
772-
fclose(in);
775+
if (in != stdin)
776+
fclose(in);
773777

774778
if (ret)
775779
return error(_("could not parse patch '%s'"), *paths);

refs/files-backend.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,8 +3294,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
32943294

32953295
/* Jump to the end */
32963296
if (fseek(logfp, 0, SEEK_END) < 0)
3297-
return error("cannot seek back reflog for %s: %s",
3298-
refname, strerror(errno));
3297+
ret = error("cannot seek back reflog for %s: %s",
3298+
refname, strerror(errno));
32993299
pos = ftell(logfp);
33003300
while (!ret && 0 < pos) {
33013301
int cnt;
@@ -3305,13 +3305,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
33053305

33063306
/* Fill next block from the end */
33073307
cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
3308-
if (fseek(logfp, pos - cnt, SEEK_SET))
3309-
return error("cannot seek back reflog for %s: %s",
3310-
refname, strerror(errno));
3308+
if (fseek(logfp, pos - cnt, SEEK_SET)) {
3309+
ret = error("cannot seek back reflog for %s: %s",
3310+
refname, strerror(errno));
3311+
break;
3312+
}
33113313
nread = fread(buf, cnt, 1, logfp);
3312-
if (nread != 1)
3313-
return error("cannot read %d bytes from reflog for %s: %s",
3314-
cnt, refname, strerror(errno));
3314+
if (nread != 1) {
3315+
ret = error("cannot read %d bytes from reflog for %s: %s",
3316+
cnt, refname, strerror(errno));
3317+
break;
3318+
}
33153319
pos -= cnt;
33163320

33173321
scanp = endp = buf + cnt;

server-info.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ static int update_info_file(char *path, int (*generate)(FILE *))
1414
char *tmp = mkpathdup("%s_XXXXXX", path);
1515
int ret = -1;
1616
int fd = -1;
17-
FILE *fp = NULL;
17+
FILE *fp = NULL, *to_close;
1818

1919
safe_create_leading_directories(path);
2020
fd = git_mkstemp_mode(tmp, 0666);
2121
if (fd < 0)
2222
goto out;
23-
fp = fdopen(fd, "w");
23+
to_close = fp = fdopen(fd, "w");
2424
if (!fp)
2525
goto out;
26+
fd = -1;
2627
ret = generate(fp);
2728
if (ret)
2829
goto out;
29-
if (fclose(fp))
30+
fp = NULL;
31+
if (fclose(to_close))
3032
goto out;
3133
if (adjust_shared_perm(tmp) < 0)
3234
goto out;

0 commit comments

Comments
 (0)