{"thread":{"id":"50171","subject":"[PATCH] sha1-file: close fd of empty file in map_sha1_file_1()","startedAt":"2019-01-07T16:48:11Z","lastAt":"2019-01-07T16:48:11Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"366262","messageId":"1136813c-2f14-042a-858e-2bca1aba990f@web.de","threadId":"50171","inReplyTo":null,"subject":"[PATCH] sha1-file: close fd of empty file in map_sha1_file_1()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2019-01-07T16:48:02Z","receivedAt":"2019-01-07T16:48:11Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"map_sha1_file_1() checks if the file it is about to mmap() is empty and\nerrors out in that case and explains the situation in an error message.\nIt leaks the private handle to that empty file, though.\n\nHave the function clean up after itself and close the file descriptor\nbefore exiting early.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\nPatch generated with --function-context for easier review.\n\n sha1-file.c | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/sha1-file.c b/sha1-file.c\nindex 5a272f70de..bfa7a2e121 100644\n--- a/sha1-file.c\n+++ b/sha1-file.c\n@@ -942,31 +942,32 @@ static int quick_has_loose(struct repository *r,\n /*\n  * Map the loose object at \"path\" if it is not NULL, or the path found by\n  * searching for a loose object named \"sha1\".\n  */\n static void *map_sha1_file_1(struct repository *r, const char *path,\n \t\t\t     const unsigned char *sha1, unsigned long *size)\n {\n \tvoid *map;\n \tint fd;\n \n \tif (path)\n \t\tfd = git_open(path);\n \telse\n \t\tfd = open_sha1_file(r, sha1, &path);\n \tmap = NULL;\n \tif (fd >= 0) {\n \t\tstruct stat st;\n \n \t\tif (!fstat(fd, &st)) {\n \t\t\t*size = xsize_t(st.st_size);\n \t\t\tif (!*size) {\n \t\t\t\t/* mmap() is forbidden on empty files */\n \t\t\t\terror(_(\"object file %s is empty\"), path);\n+\t\t\t\tclose(fd);\n \t\t\t\treturn NULL;\n \t\t\t}\n \t\t\tmap = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);\n \t\t}\n \t\tclose(fd);\n \t}\n \treturn map;\n }\n-- \n2.20.1\n"}]}