diff mbox series

sha1-file: close fd of empty file in map_sha1_file_1()

Message ID 1136813c-2f14-042a-858e-2bca1aba990f@web.de (mailing list archive)
State New, archived
Headers show
Series sha1-file: close fd of empty file in map_sha1_file_1() | expand

Commit Message

René Scharfe Jan. 7, 2019, 4:48 p.m. UTC
map_sha1_file_1() checks if the file it is about to mmap() is empty and
errors out in that case and explains the situation in an error message.
It leaks the private handle to that empty file, though.

Have the function clean up after itself and close the file descriptor
before exiting early.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
Patch generated with --function-context for easier review.

 sha1-file.c | 1 +
 1 file changed, 1 insertion(+)
diff mbox series

Patch

diff --git a/sha1-file.c b/sha1-file.c
index 5a272f70de..bfa7a2e121 100644
--- a/sha1-file.c
+++ b/sha1-file.c
@@ -942,31 +942,32 @@  static int quick_has_loose(struct repository *r,
 /*
  * Map the loose object at "path" if it is not NULL, or the path found by
  * searching for a loose object named "sha1".
  */
 static void *map_sha1_file_1(struct repository *r, const char *path,
 			     const unsigned char *sha1, unsigned long *size)
 {
 	void *map;
 	int fd;
 
 	if (path)
 		fd = git_open(path);
 	else
 		fd = open_sha1_file(r, sha1, &path);
 	map = NULL;
 	if (fd >= 0) {
 		struct stat st;
 
 		if (!fstat(fd, &st)) {
 			*size = xsize_t(st.st_size);
 			if (!*size) {
 				/* mmap() is forbidden on empty files */
 				error(_("object file %s is empty"), path);
+				close(fd);
 				return NULL;
 			}
 			map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
 		}
 		close(fd);
 	}
 	return map;
 }