git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] sha1-file: close fd of empty file in map_sha1_file_1()
@ 2019-01-07 16:48 René Scharfe
  0 siblings, 0 replies; only message in thread
From: René Scharfe @ 2019-01-07 16:48 UTC (permalink / raw)
  To: Git List; +Cc: Matthieu Moy, Junio C Hamano

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 --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;
 }
-- 
2.20.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2019-01-07 16:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-07 16:48 [PATCH] sha1-file: close fd of empty file in map_sha1_file_1() René Scharfe

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).