From e35f9824159bba94eecdf22d198799701ed60940 Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Fri, 29 Jul 2005 10:49:14 -0400 Subject: [PATCH] mmap error handling I have reviewed all occurrences of mmap() in git and fixed three types of errors/defects: 1) The result is not checked. 2) The file descriptor is closed if mmap() succeeds, but not when it fails. 3) Various casts applied to -1 are used instead of MAP_FAILED, which is specifically defined to check mmap() return value. [jc: This is a second round of Pavel's patch. He fixed up the problem that close() potentially clobbering the errno from mmap, which the first round had.] Signed-off-by: Pavel Roskin Signed-off-by: Junio C Hamano --- sha1_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sha1_file.c') diff --git a/sha1_file.c b/sha1_file.c index 5ec5598d7..eba5a36f2 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -518,7 +518,7 @@ static void *map_sha1_file_internal(const unsigned char *sha1, } map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); - if (-1 == (int)(long)map) + if (map == MAP_FAILED) return NULL; *size = st.st_size; return map; @@ -1363,7 +1363,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con if (size) buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); - if ((int)(long)buf == -1) + if (buf == MAP_FAILED) return -1; if (!type) -- cgit v1.2.1