git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
René Scharfe <l.s.r@web.de>
Date
Jan 7, 2019, 16:48 UTC
Message-ID
<1136813c-2f14-042a-858e-2bca1aba990f@web.de>

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
Message 1 of 1 in “sha1-file: close fd of empty file in map_sha1_file_1()”
  1. sha1-file: close fd of empty file in map_sha1_file_1()René Scharfe, Jan 7, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.