{"thread":{"id":"38838","subject":"[PATCH] sha1fd_check: die when we cannot open the file","startedAt":"2015-03-18T06:30:12Z","lastAt":"2015-03-18T06:30:12Z","messageCount":1,"participants":["Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"257905","messageId":"20150318063012.GA24730@peff.net","threadId":"38838","inReplyTo":null,"subject":"[PATCH] sha1fd_check: die when we cannot open the file","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-03-18T06:30:12Z","receivedAt":"2015-03-18T06:30:12Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Right now we return a NULL \"struct sha1file\" if we encounter\nan error. However, the sole caller (write_idx_file) does not\ncheck the return value, and will segfault if we hit this\ncase.\n\nOne option would be to handle the error in the caller.\nHowever, there's really nothing for it to do but die. This\ncode path is hit during \"git index-pack --verify\"; after we\nverify the packfile, we check that the \".idx\" we would\ngenerate from it is byte-wise identical to what is on disk.\nWe hit the error (and segfault) if we can't open the .idx\nfile (a likely cause of this is that somebody else ran \"git\nrepack -ad\" while we were verifying). Since we can't\ncomplete the requested verification, we really have no\nchoice but to die.\n\nFurthermore, the rest of the sha1fd_* functions simply die\non errors. So if were to open the file successfully, for\nexample, and then hit a read error, sha1write would call\ndie() for us. So pushing the die() down into sha1fd_check\nkeeps the interface consistent.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n csum-file.c | 10 +++-------\n 1 file changed, 3 insertions(+), 7 deletions(-)\n\ndiff --git a/csum-file.c b/csum-file.c\nindex b00b215..a172199 100644\n--- a/csum-file.c\n+++ b/csum-file.c\n@@ -130,14 +130,10 @@ struct sha1file *sha1fd_check(const char *name)\n \n \tsink = open(\"/dev/null\", O_WRONLY);\n \tif (sink < 0)\n-\t\treturn NULL;\n+\t\tdie_errno(\"unable to open /dev/null\");\n \tcheck = open(name, O_RDONLY);\n-\tif (check < 0) {\n-\t\tint saved_errno = errno;\n-\t\tclose(sink);\n-\t\terrno = saved_errno;\n-\t\treturn NULL;\n-\t}\n+\tif (check < 0)\n+\t\tdie_errno(\"unable to open '%s'\", name);\n \tf = sha1fd(sink, name);\n \tf->check_fd = check;\n \treturn f;\n-- \n2.3.3.520.g3cfbb5d\n"}]}