[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 <proski@gnu.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Pavel Roskin
2005-07-29 10:49:14 -04:00
committed by Junio C Hamano
parent 1df092d211
commit e35f982415
8 changed files with 15 additions and 12 deletions

View File

@ -392,7 +392,7 @@ int read_cache(void)
return (errno == ENOENT) ? 0 : error("open failed");
size = 0; // avoid gcc warning
map = (void *)-1;
map = MAP_FAILED;
if (!fstat(fd, &st)) {
size = st.st_size;
errno = EINVAL;
@ -400,7 +400,7 @@ int read_cache(void)
map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
}
close(fd);
if (-1 == (int)(long)map)
if (map == MAP_FAILED)
return error("mmap failed");
hdr = map;