@matheusbsbs / matheus-bagliano / commits / 91c8d5905c
Fork 0

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 <jim@meyering.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jim Meyering committed Jun 24, 2007 at 21:20 UTC 91c8d5905c33e9d7c9014a1c6c7cec8eb86584df
4 files changed +8 -7
builtin-init-db.c
+2 -1
@@ -40,7 +40,8 @@ static int copy_file(const char *dst, const char *src, int mode)
40 return fdo;
41 }
42 status = copy_fd(fdi, fdo);
43 - close(fdo);
43 + if (close(fdo) != 0)
44 + return error("%s: write error: %s", dst, strerror(errno));
45
46 if (!status && adjust_shared_perm(dst))
47 return -1;
builtin-rerere.c
+2 -1
@@ -57,7 +57,8 @@ static int write_rr(struct path_list *rr, int out_fd)
57 write_in_full(out_fd, path, length) != length)
58 die("unable to write rerere record");
59 }
60 - close(out_fd);
60 + if (close(out_fd) != 0)
61 + die("unable to write rerere record");
62 return commit_lock_file(&write_lock);
63 }
64
index-pack.c
+2 -1
@@ -634,7 +634,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
634 write_or_die(keep_fd, keep_msg, keep_msg_len);
635 write_or_die(keep_fd, "\n", 1);
636 }
637 - close(keep_fd);
637 + if (close(keep_fd) != 0)
638 + die("cannot write keep file");
639 report = "keep";
640 }
641 }
refs.c
+2 -4
@@ -1106,8 +1106,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
1106 len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
1107 written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
1108 free(logrec);
1109 - close(logfd);
1110 - if (written != len)
1109 + if (close(logfd) != 0 || written != len)
1110 return error("Unable to append to %s", log_file);
1111 return 0;
1112 }
@@ -1204,8 +1203,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
1203 goto error_free_return;
1204 }
1205 written = write_in_full(fd, ref, len);
1207 - close(fd);
1208 - if (written != len) {
1206 + if (close(fd) != 0 || written != len) {
1207 error("Unable to write to %s", lockpath);
1208 goto error_unlink_return;
1209 }