Skip to content

Commit 91c8d59

Browse files
meyeringgitster
authored andcommitted
detect close failure on just-written file handles
I audited git for potential undetected write failures. In the cases fixed below, the diagnostics I add mimic the diagnostics used in surrounding code, even when that means not reporting the precise strerror(errno) cause of the error. Signed-off-by: Jim Meyering <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2275d50 commit 91c8d59

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

builtin-init-db.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ static int copy_file(const char *dst, const char *src, int mode)
4040
return fdo;
4141
}
4242
status = copy_fd(fdi, fdo);
43-
close(fdo);
43+
if (close(fdo) != 0)
44+
return error("%s: write error: %s", dst, strerror(errno));
4445

4546
if (!status && adjust_shared_perm(dst))
4647
return -1;

builtin-rerere.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static int write_rr(struct path_list *rr, int out_fd)
5757
write_in_full(out_fd, path, length) != length)
5858
die("unable to write rerere record");
5959
}
60-
close(out_fd);
60+
if (close(out_fd) != 0)
61+
die("unable to write rerere record");
6162
return commit_lock_file(&write_lock);
6263
}
6364

index-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
634634
write_or_die(keep_fd, keep_msg, keep_msg_len);
635635
write_or_die(keep_fd, "\n", 1);
636636
}
637-
close(keep_fd);
637+
if (close(keep_fd) != 0)
638+
die("cannot write keep file");
638639
report = "keep";
639640
}
640641
}

refs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
11061106
len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
11071107
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
11081108
free(logrec);
1109-
close(logfd);
1110-
if (written != len)
1109+
if (close(logfd) != 0 || written != len)
11111110
return error("Unable to append to %s", log_file);
11121111
return 0;
11131112
}
@@ -1204,8 +1203,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
12041203
goto error_free_return;
12051204
}
12061205
written = write_in_full(fd, ref, len);
1207-
close(fd);
1208-
if (written != len) {
1206+
if (close(fd) != 0 || written != len) {
12091207
error("Unable to write to %s", lockpath);
12101208
goto error_unlink_return;
12111209
}

0 commit comments

Comments
 (0)