{"thread":{"id":"8700","subject":"[PATCH] detect close failure on just-written file handles","startedAt":"2007-06-24T19:20:41Z","lastAt":"2007-06-24T19:20:41Z","messageCount":1,"participants":["Jim Meyering"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"45685","messageId":"873b0hyx9y.fsf@rho.meyering.net","threadId":"8700","inReplyTo":null,"subject":"[PATCH] detect close failure on just-written file handles","fromName":"Jim Meyering","fromEmail":"jim@meyering.net","sentAt":"2007-06-24T19:20:41Z","receivedAt":"2007-06-24T19:20:41Z","isPatch":true,"sender":{"key":"jim@meyering.net","avatar":"https://avatars.githubusercontent.com/u/710630?v=4"},"body":"I audited git for potential undetected write failures.\nIn the cases fixed below, the diagnostics I add mimic the diagnostics\nused in surrounding code, even when that means not reporting\nthe precise strerror(errno) cause of the error.\n\nSigned-off-by: Jim Meyering <jim@meyering.net>\n---\n builtin-init-db.c |    3 ++-\n builtin-rerere.c  |    3 ++-\n index-pack.c      |    3 ++-\n refs.c            |    6 ++----\n 4 files changed, 8 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin-init-db.c b/builtin-init-db.c\nindex 0be2d2e..976f47b 100644\n--- a/builtin-init-db.c\n+++ b/builtin-init-db.c\n@@ -40,7 +40,8 @@ static int copy_file(const char *dst, const char *src, int mode)\n \t\treturn fdo;\n \t}\n \tstatus = copy_fd(fdi, fdo);\n-\tclose(fdo);\n+\tif (close(fdo) != 0)\n+\t\treturn error(\"%s: write error: %s\", dst, strerror(errno));\n\n \tif (!status && adjust_shared_perm(dst))\n \t\treturn -1;\ndiff --git a/builtin-rerere.c b/builtin-rerere.c\nindex f6409b9..29fb075 100644\n--- a/builtin-rerere.c\n+++ b/builtin-rerere.c\n@@ -57,7 +57,8 @@ static int write_rr(struct path_list *rr, int out_fd)\n \t\t    write_in_full(out_fd, path, length) != length)\n \t\t\tdie(\"unable to write rerere record\");\n \t}\n-\tclose(out_fd);\n+\tif (close(out_fd) != 0)\n+\t\tdie(\"unable to write rerere record\");\n \treturn commit_lock_file(&write_lock);\n }\n\ndiff --git a/index-pack.c b/index-pack.c\nindex 82c8da3..8403c36 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -634,7 +634,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,\n \t\t\t\twrite_or_die(keep_fd, keep_msg, keep_msg_len);\n \t\t\t\twrite_or_die(keep_fd, \"\\n\", 1);\n \t\t\t}\n-\t\t\tclose(keep_fd);\n+\t\t\tif (close(keep_fd) != 0)\n+\t\t\t\tdie(\"cannot write keep file\");\n \t\t\treport = \"keep\";\n \t\t}\n \t}\ndiff --git a/refs.c b/refs.c\nindex 67ac97c..4dc7e8b 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1106,8 +1106,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,\n \t\tlen += sprintf(logrec + len - 1, \"\\t%.*s\\n\", msglen, msg) - 1;\n \twritten = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;\n \tfree(logrec);\n-\tclose(logfd);\n-\tif (written != len)\n+\tif (close(logfd) != 0 || written != len)\n \t\treturn error(\"Unable to append to %s\", log_file);\n \treturn 0;\n }\n@@ -1204,8 +1203,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,\n \t\tgoto error_free_return;\n \t}\n \twritten = write_in_full(fd, ref, len);\n-\tclose(fd);\n-\tif (written != len) {\n+\tif (close(fd) != 0 || written != len) {\n \t\terror(\"Unable to write to %s\", lockpath);\n \t\tgoto error_unlink_return;\n \t}\n"}]}